Created
February 19, 2011 07:59
-
-
Save oem/834915 to your computer and use it in GitHub Desktop.
Use method_missing to dynamically create "faked" class methods
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
# method missing fun to fake class methods | |
class Foo | |
def tell(something) | |
puts something | |
puts yield | |
end | |
def self.method_missing(name, *args, &block) | |
if instance_methods.include? name | |
foo = Foo.new | |
foo.send(name, args, &block) | |
end | |
end | |
end | |
Foo.tell "moo" do | |
"moo" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment