Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created February 5, 2009 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/58751 to your computer and use it in GitHub Desktop.
Save hitode909/58751 to your computer and use it in GitHub Desktop.
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