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 > Class - Accessing Parent Methods from Child
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Class - Accessing Parent Methods from Child

Réponse
 
LinkBack Outils de la discussion
Vieux 03/12/2007, 01h04   #1
Michael Boutros
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Class - Accessing Parent Methods from Child

Hello everyone, I'm working on an API for Google Reader, and I'm having
some trouble with classes and inheritance and the like. I've got my main
class, Reader, and then two children classes, Subscription < Reader and
Item < Reader. Now, in my instance of Reader, I have two class variables
set: @headers, which are the headers that are to always be included in
the Net::HTTP call, and @connection, the Net::HTTP instance that is
connected to the Google Reader API.

I then have a method called get_page in Reader, which takes a URL and
any custom headers, then does the call. All this works perfectly well,
when #get_page is called from Reader. However, I also want to call
get_page from Subscription and Item. However, understandably, when I
call it from Subscription, I get an error that @connection and @headers
are undefined. I don't know where to start or what to do.

Any ideas?

Thanks,
Michael Boutros
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 03/12/2007, 03h31   #2
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class - Accessing Parent Methods from Child

On Dec 2, 6:04 pm, Michael Boutros <m...@michaelboutros.com> wrote:
> Hello everyone, I'm working on an API for Google Reader, and I'm having
> some trouble with classes and inheritance and the like. I've got my main
> class, Reader, and then two children classes, Subscription < Reader and
> Item < Reader. Now, in my instance of Reader, I have two class variables
> set: @headers, which are the headers that are to always be included in
> the Net::HTTP call, and @connection, the Net::HTTP instance that is
> connected to the Google Reader API.
>
> I then have a method called get_page in Reader, which takes a URL and
> any custom headers, then does the call. All this works perfectly well,
> when #get_page is called from Reader. However, I also want to call
> get_page from Subscription and Item. However, understandably, when I
> call it from Subscription, I get an error that @connection and @headers
> are undefined. I don't know where to start or what to do.


class Reader
HEADERS = "<headers>"
@@connection1 = "con1"
@connection2 = "con2"
class << self
attr_reader :connection2
end

def do_things
p HEADERS
p @@connection1
p self.class.connection2
end
end



class Subscription < Reader
def do_things
p HEADERS
p @@connection1
p self.class.connection2
p self.class.superclass.connection2
p Reader.connection2
end
end

Reader.new.do_things
#=> "<headers>"
#=> "con1"
#=> "con2"

Subscription.new.do_things
#=> "<headers>"
#=> "con1"
#=> nil
#=> "con2"
#=> "con2"


Conclusion: If you want to access class-level information from a
superclass, the simplest way is to use a constant (if the value is
constant). If the value needs to change, you can use a
@@class_variable (which is shared between the superclass and all its
descendants) or you can explicitly find the class you're interested in.
  Réponse avec citation
Vieux 03/12/2007, 06h58   #3
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Class - Accessing Parent Methods from Child

Hi --

On Mon, 3 Dec 2007, Michael Boutros wrote:

> Hello everyone, I'm working on an API for Google Reader, and I'm having
> some trouble with classes and inheritance and the like. I've got my main
> class, Reader, and then two children classes, Subscription < Reader and
> Item < Reader. Now, in my instance of Reader, I have two class variables
> set: @headers, which are the headers that are to always be included in
> the Net::HTTP call, and @connection, the Net::HTTP instance that is
> connected to the Google Reader API.
>
> I then have a method called get_page in Reader, which takes a URL and
> any custom headers, then does the call. All this works perfectly well,
> when #get_page is called from Reader. However, I also want to call
> get_page from Subscription and Item. However, understandably, when I
> call it from Subscription, I get an error that @connection and @headers
> are undefined. I don't know where to start or what to do.


The variables you've mentioned (@headers and @connection) are instance
variables, not class variables. Class variables have two @@'s.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!

  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 02h23.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09244 seconds with 11 queries