Posted By

Shamaoke on 03/07/11


Tagged

ruby boundmethod unboundmethod


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

WaxWing


Bind the parent's method to the child objects


 / Published in: Ruby
 

  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  

You need to login to post a comment.