Skip to content

Commit

Permalink
Merge pull request #110 from project-eutopia/add_set_random_method_to…
Browse files Browse the repository at this point in the history
…_context

Add set random method to context
  • Loading branch information
project-eutopia committed Apr 29, 2021
2 parents 00b728a + a09805e commit a641c6b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/keisan/context.rb
Expand Up @@ -111,7 +111,11 @@ def register_function!(name, function, local: false)
end

def random
@random || @parent&.random || Random.new
@random ||= @parent&.random || Random.new
end

def set_random(random)
@random = random
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/keisan/version.rb
@@ -1,3 +1,3 @@
module Keisan
VERSION = "0.8.6"
VERSION = "0.8.7"
end
19 changes: 19 additions & 0 deletions spec/keisan/context_spec.rb
Expand Up @@ -166,6 +166,25 @@

expect(matches.any? {|bool| bool == false}).to be true
end

it "has the random object set once and only once" do
context = described_class.new
random = context.random
expect(random).to eq context.random
end

it "can be set to override existing internal random object" do
rand1 = Random.new(2244)
rand1_copy = Random.new(2244)
rand2 = Random.new(4466)

context = described_class.new(random: rand2)
context.set_random(rand1_copy)

20.times do
expect(context.random.rand(100)).to eq rand1.rand(100)
end
end
end

describe "has_variable?" do
Expand Down
2 changes: 1 addition & 1 deletion spec/version_spec.rb
Expand Up @@ -2,6 +2,6 @@

RSpec.describe Keisan do
it "has the expected version number" do
expect(Keisan::VERSION).to eq "0.8.6"
expect(Keisan::VERSION).to eq "0.8.7"
end
end

0 comments on commit a641c6b

Please sign in to comment.