Base class method Need base class value

Discussion in 'Ruby' started by Karan Rajput, Dec 22, 2010.

  1. Karan Rajput

    Karan Rajput Guest

    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/.
     
    Karan Rajput, Dec 22, 2010
    #1
    1. Advertising

  2. Karan Rajput

    OZAWA Sakuro Guest

    On Thu, Dec 23, 2010 at 00:22, Karan Rajput <> 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
     
    OZAWA Sakuro, Dec 22, 2010
    #2
    1. Advertising

  3. Karan Rajput

    Abinoam Jr. Guest

    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 <> wrote:
    > On Thu, Dec 23, 2010 at 00:22, Karan Rajput <> 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
    >
    >
     
    Abinoam Jr., Dec 22, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Teis Draiby
    Replies:
    3
    Views:
    451
    Pete Vidler
    Apr 2, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    549
    John Carson
    Sep 3, 2005
  3. tomy
    Replies:
    5
    Views:
    450
    Marcus Kwok
    Jul 24, 2006
  4. Hicham Mouline
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Apr 20, 2009
  5. Tinku
    Replies:
    3
    Views:
    1,177
    Alf P. Steinbach
    Jan 31, 2010
Loading...

Share This Page