title: Recursive method with condition
body: <p>Using Ruby 2.0. Here's my code:</p>
  
  <pre><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 &lt; A
      def foo
        @x += 2
        super
      end
    end
  end
  
  module Test
    class C &lt; A
      def foo
        @x += 1
        super
      end
    end
  end
  
  def update(x)
    if x &gt; 100
      Test::B.new(x)
    else
      Test::C.new(x)
    end
  end
  
  x = 100
  update(x)
  </code></pre>
  
  <p>Here's what I am trying to achieve:</p>
  
  <p><strong>In <code>Test::A#foo</code>, <code>update(@x)</code> should be called only if it were called from <code>Test::C#foo</code> or it's a <code>Test::C</code> object.</strong></p>
  
  <p>The first time <code>update(x)</code> is called, <code>x = 100</code>, so the operation should <code>+1</code> and then move on to <code>Test::B#foo</code> once. How do I achieve this?</p>