Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. # No yielding
  2.  
  3. class Person
  4. attr_accessor :first, :last
  5. def initialize(first, last)
  6. @first = first
  7. @last = last
  8. end
  9. def hello
  10. puts "#{@first} #{@last} says hello!"
  11. end
  12. end
  13.  
  14. martin = Person.new("Martin", "Heimbring")
  15. martin.hello # Martin Heimbring says hello!
  16.  
  17. martin = Person.new
  18. martin.first = "Martin"
  19. martin.last = "Heimbring"
  20. martin.hello # Martin Heimbring says hello!
  21.  
  22.  
  23. class YieldPerson
  24. attr_accessor :first, :last
  25. def initialize(first, last)
  26. @first = first
  27. @last = last
  28. yield self if block_given?
  29. end
  30. def hello
  31. puts "#{@first} #{@last} says hello!"
  32. end
  33. end
  34.  
  35. # While both of the previous ways work, so does this...
  36. YieldPerson.new do |p|
  37. p.first = "Martin"
  38. p.last = "Heimbring"
  39. p.hello # Martin Heimbring says hello!
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement