Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # method that takes a block and
- # might execute it.
- #
- # Good to use when users are expecting
- # some some sort of output but you want to
- # keep them on their toes-- stupid users.
- def maybe(&block)
- if(rand(2) == 0)
- block.call
- end
- end
- #usage
- maybe do
- puts "this /might/ get executed"
- # do_something
- end
- maybe do
- puts "this /might/ also get executed"
- # do_something else
- maybe do
- puts "You can also have nexted levels of possible execution... possibly."
- end
- end
- list = [1,2,3,4,5,6,7, 10]
- #print some/none/all of the numbers in list
- list.each do |elem| maybe do
- #you can even curry maybe into other blocks
- puts elem.to_s
- end
- end
Add Comment
Please, Sign In to add comment