You can do stuff like this:
class Object
def foo
puts "Bar"
end
end
u = Object.instance_method :foo
class Object
undef foo
end
class ArbitraryClass
end
o = ArbitraryClass.new
b = u.bind o
b.call
Which isn't as generally useful, I know, but if you're constructing methods
from strings which you seem to be doing elsewhere, you can do something
like this:
class Object
def Object.universal_method(args, code)
eval %Q/
def method_44518982938293(#{args})
#{code}
end
/
m = instance_method :method_44518982938293
undef method_44518982938293
m
end
end
u = Object.universal_method %Q{foo, bar}, %Q{
puts foo, bar
}
class ArbitrarySubclass
end
o = ArbitrarySubclass.new
b = u.bind o
b.call 2, 3
Not exactly elegant code, but it works.
- Dan