Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lsegal/force_bind
Adds UnboundMethod#bind_class to bind an UnboundMethod to a class.
https://github.com/lsegal/force_bind
Last synced: about 2 months ago
JSON representation
Adds UnboundMethod#bind_class to bind an UnboundMethod to a class.
- Host: GitHub
- URL: https://github.com/lsegal/force_bind
- Owner: lsegal
- Created: 2009-07-15T06:23:57.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2011-01-10T21:09:59.000Z (almost 14 years ago)
- Last Synced: 2024-10-31T06:51:31.371Z (2 months ago)
- Language: C
- Homepage:
- Size: 606 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README
Awesome Lists containing this 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 #"
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
#=> #