Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Base class method Need base class value

Reply
Thread Tools

Base class method Need base class value

 
 
Karan Rajput
Guest
Posts: n/a
 
      12-22-2010
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
OZAWA Sakuro
Guest
Posts: n/a
 
      12-22-2010
On Thu, Dec 23, 2010 at 00:22, Karan Rajput <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Abinoam Jr.
Guest
Posts: n/a
 
      12-22-2010
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()


On Wed, Dec 22, 2010 at 1:23 PM, OZAWA Sakuro <(E-Mail Removed)> wrote:
> On Thu, Dec 23, 2010 at 00:22, Karan Rajput <(E-Mail Removed)> wrot=

e:
>
>> 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
> =A0 Base.instance_method(:age).bind(self).call
> end
>
> --
> OZAWA Sakuro
>
> "I think we can agree, the past is over." - George W. Bush
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling base class method from derive class object without virtual Tinku C++ 3 01-31-2010 02:40 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Base class method that returns a pointer to a derived class? Teis Draiby C++ 3 04-02-2004 12:59 PM



Advertisments