Created
January 12, 2011 23:53
-
-
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
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
| # 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