Using Ruby 2.0. Here's my code:
module Test
class A
def initialize(x)
@x = x
foo
end
def foo
p @x
update(@x) # only if it's a Test::C object or called from Test::C
end
end
end
module Test
class B < A
def foo
@x += 2
super
end
end
end
module Test
class C < A
def foo
@x += 1
super
end
end
end
def update(x)
if x > 100
Test::B.new(x)
else
Test::C.new(x)
end
end
x = 100
update(x)
Here's what I am trying to achieve:
In Test::A#foo, update(@x) should be called only if it were called from Test::C#foo or it's a Test::C object.
The first time update(x) is called, x = 100, so the operation should +1 and then move on to Test::B#foo once. How do I achieve this?
Test::Cobject"?updateat the highest level but then call it insideTest::A#fooand you never actually call thefoomethod anywhere so nothing is actually happening here. IfTest::A#fooshould only be called fromTest::C#foothen that is where you implement it. IfTest::Adoes not need to update andTest::Bdoes not need to update why would you put the logic at such a high level? Also this does not seem to have anything to do with recursion at all