Created
April 3, 2012 17:49
-
-
Save mark/2294093 to your computer and use it in GitHub Desktop.
Send an array of method names
This file contains hidden or 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
class Object | |
def send_key_path(key_path) | |
key_path.inject(self) { |current, method| current.send(method) } | |
end | |
end | |
class Foo | |
def bar | |
Bar.new | |
end | |
end | |
class Bar | |
def baz | |
Baz.new | |
end | |
end | |
class Baz | |
def quux | |
"quux" | |
end | |
end | |
obj = Foo.new | |
puts obj.send_key_path "bar.baz.quux".split('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment