Created
November 4, 2008 06:41
-
-
Save wuputah/22094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Challenge: | |
# change foo(bar) into bar.foo | |
# | |
# Why this is hard: | |
# method foo is a method of self, which we wouldnt | |
# have access to from within any method we create | |
class Object | |
def invoke(scope, method) | |
scope.instance_eval { send(method, self) } | |
end | |
end | |
#=> foo(bar) becomes bar.invoke(self, :foo) | |
class Object | |
def method_missing(method, *args, &block) | |
if args.size == 1 | |
args.first.instance_eval { send(method, self) } | |
else | |
super | |
end | |
end | |
end | |
#=> foo(bar) becomes bar.foo(self) | |
# As an added bonus, we dont have to always pass 'self'. | |
# For instance, Time.parse(string) becomes string.parse(Time), | |
# which is kind of nifty. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment