Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class Help - Accessing Parent Methods from Child

Reply
Thread Tools

Class Help - Accessing Parent Methods from Child

 
 
Michael Boutros
Guest
Posts: n/a
 
      12-03-2007
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/.

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      12-03-2007
On Dec 2, 6:04 pm, Michael Boutros <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      12-03-2007
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
child jitter vs parent shaping vs child class-default on 871 and 1841 Robertas Cisco 0 02-23-2007 01:28 PM
Call child class methods in parent class Martin Herbert Dietze C++ 9 05-04-2005 02:41 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments