lab19: init
This commit is contained in:
20
lab19/with-prob.rb
Normal file
20
lab19/with-prob.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
def with_prob (prob)
|
||||
yield if (Random.rand < prob)
|
||||
end
|
||||
|
||||
with_prob 0.42 do
|
||||
puts "There is a 42% chance that this code will print"
|
||||
end
|
||||
|
||||
|
||||
|
||||
def foo x
|
||||
with_prob 0.5 do
|
||||
puts "Executing with_prob block"
|
||||
return 0
|
||||
end
|
||||
return x
|
||||
end
|
||||
|
||||
puts "Foo is #{foo 1}"
|
||||
|
||||
Reference in New Issue
Block a user