【发布时间】:2014-12-19 21:39:49
【问题描述】:
如果尚未定义方法,我有一个定义方法的模块。这就是 ActiveRecord 的属性的情况,因为它们的 getter 和 setter 没有定义为方法。
module B
def create_say_hello_if_not_exists
puts respond_to?(:say_hello)
define_method :say_hello do
puts 'hello'
end unless respond_to?(:say_hello)
end
end
class A
def say_hello
puts 'hi'
end
puts respond_to?(:say_hello, true)
extend B
create_say_hello_if_not_exists
end
A.new.say_hello
预期的结果是hi,但ruby 打印出hello。为什么?
【问题讨论】:
标签: ruby