Skip to content

Instantly share code, notes, and snippets.

@foogoof
Created August 7, 2010 01:31
Show Gist options
  • Save foogoof/512312 to your computer and use it in GitHub Desktop.
Save foogoof/512312 to your computer and use it in GitHub Desktop.
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