# Accessors: Problem in accessing an attribute of other class

**URL:** <https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251>\
**Category:** Ruby\
**Created:** [June 2, 2008, 9:33am UTC](https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251 "2008-06-02T09:33:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 2, 2008, 9:33am UTC](https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251/1 "2008-06-02T09:33:20Z")

</div>

Hi,

I was practising accessors in ruby and came across the following  
problem:

class Music

attr\_accessor :song, :artist  
def initialize  
@song = “This never happened before”  
@artist = “Paul McCartney”  
end  
end

class Sport \< Music

attr\_reader :player  
attr\_accessor :game, :gender  
def initialize(player)  
@player = player  
@game = {:game1 =\> ‘Tennis’, :game2 =\> ‘Cricket’, :game3 =\> ‘F1’}  
@gender = {:g1 =\> 'male, :g2 =\> ‘female’}  
end  
end

sp = Sport.new(‘Roger Federer’)  
puts sp.player =\> outputs to “Roger Federer”  
puts sp.game[:game1] =\> outputs to “Tennis”  
puts sp.gender[:g1] =\> outputs to “male”  
puts sp.song =\> outputs to “Nil” =\> I want to access the song attribute  
of Music class so that it shows me the output as “This never happened  
before”

Can anyone help so that I can access the attributes of other class?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 2, 2008, 9:36am UTC](https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251/2 "2008-06-02T09:36:53Z")

</div>

On Monday 02 June 2008, Anukul S. wrote:

> ```
> @artist = "Paul McCartney"
> @gender = {:g1 => 'male, :g2 => 'female'}
> 
> ```
> 
> Can anyone help so that I can access the attributes of other class?
> 
> Thanks,  
> Anukul

You forgot to call super from Sport#initialize.

Stefano

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 2, 2008, 9:43am UTC](https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251/3 "2008-06-02T09:43:23Z")

</div>

Hi Anukul,

Sorry, but your classes don’t make any sense. There is no reason for  
Sport to inherit from Music. Likewise, Sport shouldn’t have the  
attributes player, game, and gender. Perhaps you should brush up on  
Object Oriented programming before trying to learn ruby.

Paul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [June 2, 2008, 9:55am UTC](https://www.ruby-forum.com/t/accessors-problem-in-accessing-an-attribute-of-other-class/139251/4 "2008-06-02T09:55:30Z")

</div>

Stefano C. wrote:

> On Monday 02 June 2008, Anukul S. wrote:
> 
> > ```
> > @artist = "Paul McCartney"
> > @gender = {:g1 => 'male, :g2 => 'female'}
> > 
> > ```
> > 
> > Can anyone help so that I can access the attributes of other class?
> > 
> > Thanks,  
> > Anukul
> 
> You forgot to call super from Sport#initialize.
> 
> Stefano

Thanks a lot stefano!!
