インスタンスの任意のメソッドを実行。
Object#sendでインスタンスの任意のメソッドを実行できます。
サンプル:
class Mii def meow ( times ) times.times { print "meow!" } end def sleep ( times, somniloquy ) times.times { print "zzz." } print somniloquy end def jump ( &block ) print "jump" yield end end mii = Mii.new mii.send("meow", 3)
出力:
meow!meow!meow!
複数の引数を渡す場合は次のようになります。
mii.send("sleep", 3, "I'm hungry.")
出力:
zzz.zzz.zzz.I'm hungry.
「*」による引数の展開を利用すれば、こんな風にもかけます。
args = [3, "I'm hungry."] mii.send("sleep", *args)
ブロックも使えます。
mii.send("jump") { print " and attack!" }
出力:
jump and attack!