Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. # Example: passing a proc into a method
  2. def give_me_a_proc(pr)
  3. puts 'Before call the proc'
  4. pr.call
  5. puts 'After calling the proc'
  6. end
  7.  
  8. noisy_proc = proc { puts 'Called me noisy' }
  9.  
  10. give_me_a_proc(noisy_proc)
  11.  
  12. # The above code will print:
  13. # Before call the proc
  14. # Called me noisy
  15. # After calling the proc
  16.  
  17. # Example: returning a proc from a method
  18. def fancy_proc_maker
  19. proc { puts 'Called me fancy' }
  20. end
  21.  
  22. fancy_proc = fancy_proc_maker
  23. fancy_proc.call
  24.  
  25. # The above code will print:
  26. # Called me fancy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement