Bind the parent's method to the child objects


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # encoding: utf-8
  2.  
  3. class Parent
  4. attr_reader :one, :two
  5. end
  6.  
  7. class FirstChild < Parent
  8. def initialize
  9. @one = 'first one'
  10. @two = 'first two'
  11. end
  12. end
  13.  
  14. class SecondChild < Parent
  15. def initialize
  16. @one = 'second one'
  17. @two = 'second two'
  18. end
  19. end
  20.  
  21. one = Parent.new.method(:one).unbind
  22. two = Parent.new.method(:two).unbind
  23.  
  24. puts one.bind(FirstChild.new).call #=> first one
  25. puts two.bind(FirstChild.new).call #=> first two
  26.  
  27. puts one.bind(SecondChild.new).call #=> second one
  28. puts two.bind(SecondChild.new).call #=> second two

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.