How to access a attributes from the parent class?

Discussion in 'Ruby' started by Paatsch, Bernd, Jan 25, 2006.

  1. ------_=_NextPart_001_01C62148.964DD45C
    Content-Type: text/plain

    I am new to ruby and try to figure out how inheritance works.
    I don't get the result I expect. I marked the line of code with "#error".
    What do I need to do to get it to work?

    class WacFrames
    attr_reader :wacFrameNav
    attr_reader :wacFrameDisp
    def initialize()
    @wacFrameNav = "nav"
    @wacFrameDisp = "disp"
    end
    end

    class WacSubscribers
    attr_reader :fr
    attr_reader :frame
    def initialize()
    @fr = WacFrames.new()
    @frame = @fr.wacFrameNav
    end
    end

    class WacSubProfile < WacSubscribers
    def initialize()
    #some code
    end
    end
    end

    another = WacSubscribers.new()
    puts another.fr #works fine
    puts another.frame #works fine

    test = WacSubProfile.new()
    puts test.fr #error
    puts test.frame #error


    Thanks

    ------_=_NextPart_001_01C62148.964DD45C--
     
    Paatsch, Bernd, Jan 25, 2006
    #1
    1. Advertising

  2. Paatsch, Bernd wrote:
    > I am new to ruby and try to figure out how inheritance works.
    > I don't get the result I expect. I marked the line of code with
    > "#error".
    > What do I need to do to get it to work?
    >
    > <skip WacFrames />
    >
    > class WacSubscribers
    > attr_reader :fr
    > attr_reader :frame
    > def initialize()
    > @fr = WacFrames.new()
    > @frame = @fr.wacFrameNav
    > end
    > end
    >
    > class WacSubProfile < WacSubscribers
    > def initialize()
    > #some code
    > end
    > end
    > end


    An extra 'end' there but I assume it is just a copy-paste error.

    > another = WacSubscribers.new()
    > puts another.fr #works fine
    > puts another.frame #works fine
    >
    > test = WacSubProfile.new()
    > puts test.fr #error
    > puts test.frame #error


    This should work fine (you might have problems if you
    do not initialize the parent's instance variables by
    calling #super but even still the methods should return
    nils).

    What error is it that you get?

    > Thanks



    E

    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Jan 25, 2006
    #2
    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. Roy
    Replies:
    2
    Views:
    3,169
  2. E11
    Replies:
    1
    Views:
    4,937
    Thomas Weidenfeller
    Oct 12, 2005
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    411
    Bruno Desthuilliers
    Jun 20, 2007
  4. metal
    Replies:
    8
    Views:
    527
    Bruno Desthuilliers
    Oct 30, 2009
  5. Bitswapper
    Replies:
    5
    Views:
    182
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page