Skip to content

Instantly share code, notes, and snippets.

@hitode909 hitode909/test.rb
Created Feb 5, 2009

Embed
What would you like to do?
class Human
def initialize(name)
@name = name
end
def define_method(name, &block)
self.class.class_eval do
define_method(name, block)
end
end
end
taro = Human.new("Taro")
taro.define_method(:who){
puts "I'm #{@name}."
}
taro.define_method(:hi){ |to|
puts "#{@name}: Hi, #{to}."
}
taro.who # => I'm Taro.
taro.hi "James" # => Taro: Hi, James.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.