Base class method Need base class value

Could anybody suggest what am i suppose to do for below problem.

Class Base

def age
return 18
end

def getOldAge
age
end
end

Class Der < Base
def age
return 19
end
end
ob = Der.new()
ob.getOldAge()

above run command will give me output as 19. Because its now overridden
by Der class and its ‘age’ method.

So,
Question is

  1. Is it possible for getOldAge method from base class to call its own
    class method age? Means which returns 18, not 19. In base class, i
    should have control for accessing only my class method regardless of
    overriding existing class by some else class.

-Thanks in advance.

On Thu, Dec 23, 2010 at 00:22, Karan R. [email protected]
wrote:

  1. Is it possible for getOldAge method from base class to call its own
    class method age? Means which returns 18, not 19. In base class, i
    should have control for accessing only my class method regardless of
    overriding existing class by some else class.

looks awkward but possible

in class Base

def getOldAge
Base.instance_method(:age).bind(self).call
end


OZAWA Sakuro

“I think we can agree, the past is over.” - George W. Bush

Try this… and let me know if it worked for you.

class Base

def age
return 18
end

def getOldAge
age
end
end

class Der < Base

alias the ancestor age methods before overriding

alias :base_age :age

def age
return 19
end

def getOldAge
base_age
end
end

ob = Der.new()
ob.getOldAge()