Subclasses and constants

L

listrecv

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?
 
J

Jim Weirich

List said:
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
 
L

listrecv

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!
 
R

Robert Dober

------=_Part_4289_28789915.1143475821643
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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=EAtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

------=_Part_4289_28789915.1143475821643--
 
L

listrecv

@@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)
 
R

Robert Dober

------=_Part_15022_18958756.1143534865264
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

@@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=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,062
Members
47,669
Latest member
johnmaxwell

Latest Threads

Top