M
Marat Kamenschikov
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? «INVALID»: «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?
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? «INVALID»: «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?