I would like to do something like the following:
class String
def fancy_thing appendix
# Just a trivial example to ensure self and params work.
# Pretend this is something complex.
self.reverse + appendix
end
end
# print_method on instance or class should spit out a string
# containing the actual code for that method
ft_code = "cob".print_method :fancy_thing
ft_code = String.print_instance_method :fancy_thing
# => "{|appendix| self.reverse + appendix }" *
# ft_code gets passed around a bit...
# exec on an object should run code (w/ parameters) as if that code is
# an instance method on that object (or class method if it's a class)
"cob".exec(ft_code, '!') #=> "boc!"
How might one code print_method and foo.exec? Preferably, they should work for any arbitrary methods, without knowing a priori where they might happen to have been defined or sourced from.
- Yes, I know methods and blocks aren't completely the same. But this is closer to what yield and call would normally take; I don't know of a better solution.