Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # No yielding
- class Person
- attr_accessor :first, :last
- def initialize(first, last)
- @first = first
- @last = last
- end
- def hello
- puts "#{@first} #{@last} says hello!"
- end
- end
- martin = Person.new("Martin", "Heimbring")
- martin.hello # Martin Heimbring says hello!
- martin = Person.new
- martin.first = "Martin"
- martin.last = "Heimbring"
- martin.hello # Martin Heimbring says hello!
- class YieldPerson
- attr_accessor :first, :last
- def initialize(first, last)
- @first = first
- @last = last
- yield self if block_given?
- end
- def hello
- puts "#{@first} #{@last} says hello!"
- end
- end
- # While both of the previous ways work, so does this...
- YieldPerson.new do |p|
- p.first = "Martin"
- p.last = "Heimbring"
- p.hello # Martin Heimbring says hello!
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement