class A
def hello
"Hello"
end
end
class B < A; end
class C < A; end
c = C.new
c.hello # > "Hello"
class A
def hello # Monkeypatching A to change `hello`.
"Goodbye"
end
end
# Hello returns a new value, even though it's an instance of C
c.hello # > "Goodbye"
1条答案
按热度按时间ej83mcc01#
Monkeypatching应该可以工作:
字符串