Object.send でメソッドを呼ぶ
ruby は変数に接頭辞がないから、perl で $obj->$method() みたいなことやるのにどうしたらいいんだろ、とか思ってたらプログラミング言語 Ruby に出てきた。
Object に send ってメソッドがあって、o.send :method_name で呼び出せる。
perl のこのコードは
my $hoge = Hoge->new; foreach my $method ( qw/one two three/ ) { print $hoge->$method() . "\n"; } package Hoge; sub new { bless {}, shift; } sub one { 1 } sub two { 2 } sub three { 3 }
ruby で書くとこんな感じ
class Hoge def one; 1; end def two; 2; end def three; 3; end end hoge = Hoge.new [:one, :two, :three].each {|x| puts hoge.send x }
今はリフレクション、メタプログラミングの章に入ってきたところ。