Forum: Ruby Subclasses and constants

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
List R. (Guest)
on 2006-03-27 10:43
(Received via mailing list)
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?
Jim W. (Guest)
on 2006-03-27 16:07
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.
List R. (Guest)
on 2006-03-27 19:29
(Received via mailing list)
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!
Robert D. (Guest)
on 2006-03-27 20:10
(Received via mailing list)
On 3/27/06, removed_email_address@domain.invalid 
<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
List R. (Guest)
on 2006-03-28 07:24
(Received via mailing list)
@@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)
Robert D. (Guest)
on 2006-03-28 12:35
(Received via mailing list)
On 3/28/06, removed_email_address@domain.invalid 
<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
This topic is locked and can not be replied to.