# Subclasses and constants

**URL:** <https://www.ruby-forum.com/t/subclasses-and-constants/87496>\
**Category:** Ruby\
**Created:** [March 27, 2006, 8:43am UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496 "2006-03-27T08:43:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 8:43am UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/1 "2006-03-27T08:43:57Z")

</div>

Often, I find myself doing:

class RemoteCommand  
def submit  
sendto(URL, …)  
end  
end

class GetStockPriceCommand  
URL = ‘http://…’  
end

Which won’t work.  
I’m forced to do:

sendto(self.class::URL, …)

which just seems akward. Same for using @@class\_variables.

Why? More importantly, is there a better workaround?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/2 "2006-03-27T14:07:01Z")

</div>

List R. wrote:

> Often, I find myself doing:
> 
> class RemoteCommand  
> def submit  
> sendto(URL, …)  
> end  
> end
> 
> class GetStockPriceCommand  
> URL = ‘http://…’  
> end
> 
> Which won’t work.  
> I’m forced to do:
> 
> sendto(self.class::URL, …)
> 
> which just seems akward. Same for using @@class\_variables.
> 
> Why? More importantly, is there a better workaround?

I assume you meant: class GetStockPriceCommand \< RemoteCommand

If so then the following will work:

class RemoteCommand  
def submit  
sendto(url, …)  
end  
def url  
self.class::URL  
end  
end

class GetStockPriceCommand \< RemoteCommand  
URL = ‘http://…’  
end

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/3 "2006-03-27T17:29:40Z")

</div>

Jim,

Right, that’s exactly what I meant, and that’s in fact the work around  
which I’ve been using.

My question is really why I need to do this, and if there is a better  
way… It forces me to decide whether I’ll ever subclass when I code  
the parent, which seems unrubyish… I’d rather just code the parent,  
and if the child changes the URL, so be it!

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 28, 2006, 5:24am UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/4 "2006-03-28T05:24:29Z")

</div>

@@class\_variables seemed to have the same behavior - if they were  
changed in the subclass, and the parent defined a method, it wouldn’t  
have access to the subclass’s definition.

(BTW - of course Constants don’t change - but different subclasses  
could have different ones - with them never changing - but I digress)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 27, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/5 "2006-03-27T18:10:51Z")

</div>

On 3/27/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the parent, which seems unrubyish… I’d rather just code the parent,  
> and if the child changes the URL, so be it!

and that is exactly why you should not use a Constant.  
Constants do not change, do they ;)?  
You should use an instance variable and define accessor methods to it.  
(or  
class variable for that matter)  
These accessor methods are then used by the superclass.  
This is a classic pattern, but I do not know its name in English, sorry.

Hope this helps  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [March 28, 2006, 10:35am UTC](https://www.ruby-forum.com/t/subclasses-and-constants/87496/6 "2006-03-28T10:35:21Z")

</div>

On 3/28/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @@class\_variables seemed to have the same behavior - if they were  
> changed in the subclass, and the parent defined a method, it wouldn’t  
> have access to the subclass’s definition.
> 
> (BTW - of course Constants don’t change - but different subclasses  
> could have different ones - with them never changing - but I digress)

No do not worry you do not, I just looked at an implementation that  
might  
fit your needs better, the conceptual discussion remains interesting  
anyhow.  
I will check out the class variable behaviour you mentioned, I was  
surprised  
about it.  
Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
