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
Post a Comment