Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created January 11, 2011 18:05
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 hopsoft/774813 to your computer and use it in GitHub Desktop.
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
# 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