2014年5月20日火曜日

Rubyで動的メソッド define_method編ヘ(^o^)ノ

Rubyでdefine_methodで動的にメソッドを作る

こんな感じ
class Foo
private_class_method def self.common_method(*syms)
syms.each do |sym|
define_method sym do
puts sym
end
end
end
common_method :murajun, :murajun1978
end
Foo.new.murajun # => murajun
Foo.new.murajun1978 # => murajun1978


共通部分をまとめるのに便利

( ̄(エ) ̄)彡☆

0 件のコメント:

コメントを投稿