I have a module that defines a method, if not already defined. This applies to ActiveRecord attributes because their recipients and setters are not defined as methods.
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
Expected result hi, but ruby ββprints hello. Why?
Perhaps related to Confused on "reply_to?" Method
source
share