From: Trans Date: 2005-11-11T06:07:13+09:00 Subject: Re: Golf, anyone? m4dc4p wrote: > Thanks for your reply - definitely the second case - "variable > inheritance." > > > include 'facet/inheritor' > > > > module Foo > > inheritor :prop, [], :+ > > end > > > > I looked at the implementation of inheritor and I wonder if you could > explain the reason for passing the ":+" symbol to the method. From the > implementation, you have this line: > > deflambda = lambda do > define_method( key ) do > defined?(super) ? super.__send__(op,obj) : obj.dup > end > ... > > Why does the "+" method need to be called on the parent? I am hoping to > be able to understand what you are doing here so I can be a better > Rubyist! 'op' is set to :+, so :+ is being called on the parents *method* of the same name, hence the array is being built via this operation as it traverses through the inheritance chain. If we used a different operator we could get a different effect: class X inheritor :x, [], :- end class Y < X inheritor :x, [], :- end X.x!.concat [1,2,3] Y.x! << 3 Y.x #=> [1,2] Inhertior is flexible so you can have any kind of inhertiable variable with any one of it's methods acting as the inheritance operator, eg. inheritor :hsh, {}, :merge inheritor :sum, 0, :+ inheritor :duck, Duck.new, :quack If you were to write it out by hand, what it boils down to (when using [] and :+) is basically this: class X @x = [] def self.x ; @x ; end end class Y < X @x = [] def self.x ; super + @x ; end end Of course getting Modules to play nice in this system is the tricky part which requires the use of class_inherit.rb. HTH, T.