On 03/26/2010 05:11 PM, Marat Kamenschikov wrote: > Assume there is class A with method save which takes one param with > default value (e.g. true). Class A instance gets save and behaves > depending on true value. > Assume there is class B with a field of type A. Calling save on this > field it should behave as if save(false) was called. > Outside of class B scope save method should behave originally. > Something like override, but just inside of one scope. > > Here is some piece of code: > class A > def save(validate = true) > validate? NVALIDサ: ォVALIDサ > end > end > > class B > # something very crazy > def initialize > @a = A.new > end > def save > @a.save > end > end > > A.new.save # => ォINVALIDサ > B.new.save # => ォVALIDサ > A.new.save # => ォINVALIDサ > > It is meant to be used in Rails application with ActiveRecord objects, > but it really doesn't matter much. > The most simple way here is probably examining caller list, but is there > a way to redef a method inside of other class' scope? What's wrong with doing class B # something very crazy def initialize @a = A.new end def save @a.save false end end as Jesus suggested? Even if you don't do that you could do class A def initialize(owner = nil) @owner = owner end def save B === @owner ? "VALID" : "INVALID" end end class B # something not so crazy def initialize @a = A.new self end def save @a.save end end Note: meta programming magic also makes code harder to read. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/