ruby - Recursive method with condition -


using ruby 2.0. here's code:

module test   class     def initialize(x)       @x = x       foo     end      def foo       p @x       update(@x) # if it's test::c object or called test::c     end   end end  module test   class b <     def foo       @x += 2       super     end   end end  module test   class c <     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 trying achieve:

in test::a#foo, update(@x) should called if called test::c#foo or it's test::c object.

the first time update(x) called, x = 100, operation should +1 , move on test::b#foo once. how achieve this?

the parent class should not have care subclasses. design have here implies needs know, broken , over-complicates things.

in case, if need trap cases:

case (self) when test::a, test::b   # ... stuff   update(@x) end 

if test::a , test::b care operation, should call when necessary:

def foo   @x += 1   super   update(x) end 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -