Created
February 5, 2009 15:35
-
-
Save hitode909/58751 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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