Skip to content

Instantly share code, notes, and snippets.

@ekhall
Created October 15, 2013 18:58
Show Gist options
  • Save ekhall/6996849 to your computer and use it in GitHub Desktop.
Save ekhall/6996849 to your computer and use it in GitHub Desktop.
This is sick...
--- ~ » irb
2.0.0p247 :001 > class MyClass
2.0.0p247 :002?> def initialize(val)
2.0.0p247 :003?> @x = val
2.0.0p247 :004?> end
2.0.0p247 :005?> def my_method
2.0.0p247 :006?> @x
2.0.0p247 :007?> end
2.0.0p247 :008?> end
=> nil
2.0.0p247 :009 > obj1 = MyClass.new(1)
=> #<MyClass:0x007fc09a096160 @x=1>
2.0.0p247 :010 > obj2 = MyClass.new(2)
=> #<MyClass:0x007fc09a07ec18 @x=2>
2.0.0p247 :011 > m = obj1.method :my_method
=> #<Method: MyClass#my_method>
2.0.0p247 :012 > m.call
=> 1
2.0.0p247 :013 > m2 = m.unbind.bind(obj2)
=> #<Method: MyClass#my_method>
2.0.0p247 :014 > m2.call
=> 2
2.0.0p247 :015 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment