Guest User

Untitled

a guest
Oct 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. ### (1) Conditional branching ###
  2. hello1 = nil
  3. unless hello1
  4. hello1 = 'hello1'
  5. end
  6.  
  7. ### (2) Use Proc ###
  8. hello2 ||= Proc.new do
  9. 'hello2'
  10. end.call
  11.  
  12. ### (3) Use lambda ###
  13. hello3 ||= lambda {
  14. 'hello3'
  15. }.call
  16.  
  17. ### (4) Separate out the method ###
  18. def func
  19. 'hello4'
  20. end
  21. hello4 ||= func
  22.  
  23. ### OUTPUT ###
  24. puts hello1 # -> "hello1" (Conditional)
  25. puts hello2 # -> "hello2" (Proc)
  26. puts hello3 # -> "hello3" (Lambda)
  27. puts hello4 # -> "hello4" (Method)
Add Comment
Please, Sign In to add comment