Skip to content

Instantly share code, notes, and snippets.

@wuputah
Created November 4, 2008 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuputah/22094 to your computer and use it in GitHub Desktop.
Save wuputah/22094 to your computer and use it in GitHub Desktop.
# 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