Created
January 11, 2011 18:05
-
-
Save hopsoft/774813 to your computer and use it in GitHub Desktop.
Dynamic Dispatch - Illustrates how to invoke methods that are unknown at design time
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
# add methods that provide the ability | |
# to dynamically call unknown methods on objects | |
def invoke(object, method_name) | |
object.send(method_name) | |
end | |
def invoke_with_args(object, method_name, *args) | |
object.send(method_name, *args) | |
end | |
# usage | |
invoke("get my length", :length) # => 13 | |
invoke("reverse me", :reverse) # => em esrever | |
invoke_with_args("remove all letter e's", :delete, "e") # => rmov all lttr 's |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment