Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created January 12, 2011 23:53
Show Gist options
  • Save hopsoft/777133 to your computer and use it in GitHub Desktop.
Save hopsoft/777133 to your computer and use it in GitHub Desktop.
Ghost Method - Illustrates how to use method_missing to support methods that aren't defined
# define our example class
class Example
# catch all calls to methods that don't exist
def method_missing(method_name, *args)
puts "You called '#{method_name}' with these arguments: #{args.inspect}"
end
end
# invoke methods that haven't been defined
Example.new.some_method # => You called 'some_method' with these arguments: []
Example.new.another_method(1, 2, 3) # => You called 'another_method' with these arguments: [1, 2, 3]
Example.new.this_is_cool(true) # => You called 'this_is_cool' with these arguments: [true]
Example.new.and_powerful # => You called 'and_powerful' with these arguments: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment