Forum: Ruby How to access a attributes from the parent class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fd870ca69f1243766bd8697ea3415df2?d=identicon&s=25 Paatsch, Bernd (Guest)
on 2006-01-25 01:46
(Received via mailing list)
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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2006-01-25 03:18
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
This topic is locked and can not be replied to.