Guest User

Untitled

a guest
Apr 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. # method that takes a block and
  2. # might execute it.
  3. #
  4. # Good to use when users are expecting
  5. # some some sort of output but you want to
  6. # keep them on their toes-- stupid users.
  7. def maybe(&block)
  8. if(rand(2) == 0)
  9. block.call
  10. end
  11. end
  12.  
  13. #usage
  14. maybe do
  15. puts "this /might/ get executed"
  16. # do_something
  17. end
  18.  
  19. maybe do
  20. puts "this /might/ also get executed"
  21. # do_something else
  22. maybe do
  23. puts "You can also have nexted levels of possible execution... possibly."
  24. end
  25. end
  26.  
  27. list = [1,2,3,4,5,6,7, 10]
  28.  
  29.  
  30. #print some/none/all of the numbers in list
  31. list.each do |elem| maybe do
  32. #you can even curry maybe into other blocks
  33. puts elem.to_s
  34. end
  35. end
Add Comment
Please, Sign In to add comment