# Class Help - Accessing Parent Methods from Child

**URL:** <https://www.ruby-forum.com/t/class-help-accessing-parent-methods-from-child/121933>\
**Category:** Ruby\
**Created:** [December 3, 2007, 2:04am UTC](https://www.ruby-forum.com/t/class-help-accessing-parent-methods-from-child/121933 "2007-12-03T02:04:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 3, 2007, 2:04am UTC](https://www.ruby-forum.com/t/class-help-accessing-parent-methods-from-child/121933/1 "2007-12-03T02:04:04Z")

</div>

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 B.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 3, 2007, 4:35am UTC](https://www.ruby-forum.com/t/class-help-accessing-parent-methods-from-child/121933/2 "2007-12-03T04:35:59Z")

</div>

On Dec 2, 6:04 pm, Michael B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 = “”  
@@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  
#=\> “”  
#=\> “con1”  
#=\> “con2”

Subscription.new.do\_things  
#=\> “”  
#=\> “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.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [December 3, 2007, 7:59am UTC](https://www.ruby-forum.com/t/class-help-accessing-parent-methods-from-child/121933/3 "2007-12-03T07:59:21Z")

</div>

Hi –

On Mon, 3 Dec 2007, Michael B. wrote:

> 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
