プログラミングの演習として、クラスを作成し、そのクラスから 2 つのオブジェクトをインスタンス化し、1 つのオブジェクトにモンキーパッチを適用し、method_missing を使用してもう 1 つのオブジェクトにモンキーパッチを適用する Ruby スニペットを作成しました。
これが取引です。これは意図したとおりに機能します。
class Monkey
def chatter
puts "I am a chattering monkey!"
end
def method_missing(m)
puts "No #{m}, so I'll make one..."
def screech
puts "This is the new screech."
end
end
end
m1 = Monkey.new
m2 = Monkey.new
m1.chatter
m2.chatter
def m1.screech
puts "Aaaaaargh!"
end
m1.screech
m2.screech
m2.screech
m1.screech
m2.screech
method_missing のパラメーターがあることに気付くでしょう。これを行ったのは、define_method を使用して不足しているメソッドを適切な名前で動的に作成することを望んでいたためです。しかし、うまくいきません。実際、define_method を静的な名前で使用しても、次のようになります。
def method_missing(m)
puts "No #{m}, so I'll make one..."
define_method(:screech) do
puts "This is the new screech."
end
end
次の結果で終了します。
ArgumentError: wrong number of arguments (2 for 1)
method method_missing in untitled document at line 9
method method_missing in untitled document at line 9
at top level in untitled document at line 26
Program exited.
エラー メッセージをより当惑させているのは、引数が 1 つしかないことですmethod_missing
。