Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created April 1, 2012 16:59
Show Gist options
  • Save betawaffle/2277058 to your computer and use it in GitHub Desktop.
Save betawaffle/2277058 to your computer and use it in GitHub Desktop.
UnboundMethod.class_exec do
def to_proc
method = self
lambda { |*args, &block| method.bind(self).call(*args, &block) }
end
end
module MyModule
def some_method arg
[self, arg]
end
end
m = Object.new.extend(MyModule).method(:some_method)
'abc'.instance_exec('123', &m) # :( doesn't work!
# => [#<Object:0x...>, '123']
'abc'.extend(MyModule).method(:some_method).call('123')
# => ['abc', '123']
@betawaffle
Copy link
Author

Allows you to do something like:

obj.instance_exec(x, y, z, &mod.instance_method(:bork))

as long as obj has mod in it's ancestors

(class << obj; self; end).ancestors.include? mod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment