[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Accessing Parent class

Phil Cooper-king

6/18/2008 1:39:00 PM

Okay I am a rails guys coming over to ruby, perhaps the wrong way
around but hey.

I know this is a painfully simple question but I tried google but I dont
really know what to search for.

is there any way for a sub class to access the instance infront of it?

totally stupid class

class Monster

BOO = 'hoo'

attr_accessor :roisin

def initialize
@roisin = 'rasp'
end

class Verbal

def growl
puts 'grrrrrrrr'
end

end

def speach
Verbal.new.growl
end

end

how can Verbal access the instance of Monster that created it?
--
Posted via http://www.ruby-....

2 Answers

Michael Guterl

6/18/2008 2:18:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Wed, Jun 18, 2008 at 9:39 AM, Phil Cooper-king <philcooperking@gmail.com>
wrote:

> Okay I am a rails guys coming over to ruby, perhaps the wrong way
> around but hey.
>
> I know this is a painfully simple question but I tried google but I dont
> really know what to search for.
>
> is there any way for a sub class to access the instance infront of it?
>
> totally stupid class
>
> class Monster
>
> BOO = 'hoo'
>
> attr_accessor :roisin
>
> def initialize
> @roisin = 'rasp'
> end
>
> class Verbal
>

attr_reader :monster
def initialize(monster)
@monster = monster
end

>
> def growl
> puts 'grrrrrrrr'
> end
>
> end
>
> def speach

Verbal.new(self).growl

> end
>
> end
>
> how can Verbal access the instance of Monster that created it?
>
>
I altered your code above, this should do what you're looking for.
Basically, you just need to pass the instance of the Monster upon creation
of the Verbal instance.

HTH,
Michael Guterl

Phil Cooper-king

6/18/2008 3:24:00 PM

0

thanks :)

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