PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Accessing Parent class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Accessing Parent class

Réponse
 
LinkBack Outils de la discussion
Vieux 18/06/2008, 14h39   #1
Phil Cooper-king
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Accessing Parent class

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-forum.com/.

  Réponse avec citation
Vieux 18/06/2008, 15h17   #2
Michael Guterl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Accessing Parent class

[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

  Réponse avec citation
Vieux 18/06/2008, 16h23   #3
Phil Cooper-king
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Accessing Parent class

thanks

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11991 seconds with 11 queries