Skip to content

Instantly share code, notes, and snippets.

@koraktor
Forked from stpettersens/method_missing_demo.rb
Created July 19, 2011 11:32
Show Gist options
  • Save koraktor/1092043 to your computer and use it in GitHub Desktop.
Save koraktor/1092043 to your computer and use it in GitHub Desktop.
Method Missing Demo
module MyModule
# Defines method_missing
module ClassMethods
def method_missing(method)
puts "Method: \"#{method}\" does not exist."
end
end
# Will add method_missing to MyModule itself
extend ClassMethods
# Will add method_missing to all modules/classes that include MyModule
def self.included(mod)
mod.extend ClassMethods
end
# Public methods.....
def self.doSomething(parameter)
...
end
def self.doSomethingElse()
...
end
end
# EOF
I want the following, as demonstrated in IRB:
> require "mymodule"
=> true
> MyModule.doSomething()
=> (output)
> MyModule.doesNotExist()
=> Method: "doesNotExist" does not exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment