クラスメソッドで新たなアクセッサを作るクラスを継承したときに特異メソッドが実行される
コメントの指摘を受けまして、タイトルを変更しました。
サンプルでやっていることが微妙なせいか、新しいタイトルがしっくりこない。
(2006/3/30)
class BarBase def self.properties(*symbols) symbols.each do |symbol| attr_accessor symbol end end end class Bar < BarBase # properties :name end b = Bar.new begin puts b.name # => Exception が発生 rescue Exception => e puts "#{e.message} (#{e.class})" end Bar.properties(:name) puts b.name = "foo" # => "foo"
そんな感じっぽい。以下、例。
うむ、奥が深い Ruby。まだまだ知らないことが沢山あるうぃ。
class BarBase def self.properties(*symbols) symbols.each do |symbol| attr_accessor symbol end end end class Bar < BarBase properties :name end b = Bar.new puts b.name = "foo" # => "foo"