jjzjj

Ruby:define_method 与 def

作为一项编程练习,我编写了一个Ruby片段,它创建了一个类,实例化了该类中的两个对象,猴子修补了一个对象,并依靠method_missing猴子修补了另一个对象。这是交易。这按预期工作:classMonkeydefchatterputs"Iamachatteringmonkey!"enddefmethod_missing(m)puts"No#{m},soI'llmakeone..."defscreechputs"Thisisthenewscreech."endendendm1=Monkey.newm2=Monkey.newm1.chatterm2.chatterdefm1.screec