Project

force_bind

0.0
No commit activity in last 3 years
No release in over 3 years
Adds UnboundMethod#force_bind to bind an unbound method to a class (or any object of any type)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme
= force_bind

Adds UnboundMethod#force_bind to bind an unbound method to class (or any object of any type).
It basically bypasses the argument type checking that #bind has.

== Requirements

* Ruby 1.9.1 or greater (does not work in Ruby 1.8.x)

== Example

Bind an instance method to its class (making it act like a class method):

  class Foo
    def bar
      puts "I'm inside #{self}"
    end
  end
  
  Foo.new.bar
  #=> "I'm inside #<Foo:0x123456>"
  
  meth = Foo.instance_method(:bar).force_bind(Foo)
  meth.call
  #=> "I'm inside Foo"
    
You can also use this to rebind the instance method of any object to any
other object:

  class A
    def foo; self end
  end
  
  class B; end
  
  meth = A.instance_method(:foo).force_bind(B.new)
  meth.call
  #=> #<B:0x123456>