Base class method Need base class value

K

Karan Rajput

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.
 
O

OZAWA Sakuro

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
 
A

Abinoam Jr.

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 =3D Der.new()
ob.getOldAge()


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top