"Jim Driscoll" <j / rjimlad.org> schrieb im Newsbeitrag
news:E66A8EC6-4F28-11D8-AE61-000393D47484 / rjimlad.org...
>
> On 25 Jan 2004, at 11:11, David Heinemeier Hansson wrote:
>
> > Is there anyway to let a subclass overwrite a constant in the
> > superclass? As in getting B.new.X in the example below to return 0. If
> > not are there any similar constructions that would do something
> > comparable? (short of converting the constant to a method).
> >
>
> You could just use a class variable (e.g. @@x) instead, the downside
> being that it *would* be variable rather than constant.

And the other downside that It would not have the desired effect:

irb(main):001:0> class Foo
irb(main):002:1>   @@val = "yeah!"
irb(main):003:1>   def val; @@val; end
irb(main):004:1> end
=> nil
irb(main):005:0> Foo.new.val
=> "yeah!"
irb(main):006:0> class Bar < Foo
irb(main):007:1>   @@val = "huh"
irb(main):008:1> end
=> "huh"
irb(main):009:0> Foo.new.val
=> "huh"
irb(main):010:0>

IOW the subclass would change the value also for the parent class's
instance.  Something that you normally don't want for constants.

Regards

    robert