Created
August 7, 2010 01:31
-
-
Save foogoof/512312 to your computer and use it in GitHub Desktop.
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
class Foo | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def method_missing(name, *args, &block) | |
return super unless name.to_s =~ /^go_/ | |
puts "Defining #{name}" | |
method = lambda { puts "hey #{@name}@#{object_id}, #{name.to_s.tr '_', ' '}" } | |
self.class.send :define_method, name, method | |
method.call | |
end | |
end | |
tony = Foo.new "tony" | |
tony.go_fish | |
tony.go_tiger | |
phil = Foo.new "phil" | |
phil.go_fish | |
phil.go_tiger | |
phil.go_to_school | |
tony.go_to_school | |
# Defining go_fish | |
# hey tony@2156971300, go fish | |
# Defining go_tiger | |
# hey tony@2156971300, go tiger | |
# hey phil@2156970160, go fish | |
# hey phil@2156970160, go tiger | |
# Defining go_to_school | |
# hey phil@2156970160, go to school | |
# hey tony@2156971300, go to school |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment