Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an overridden constant in an inheritanced class

given this code:

class A   CONST = 'A'    def initialize     puts CONST   end end  class B < A   CONST = 'B' end  A.new # => 'A' B.new # => 'A' 

I'd like B to use the CONST = 'B' definition, but I don't know how. Any ideas?

Greetings

Tom

like image 856
Tom Avatar asked Jul 04 '10 12:07

Tom


People also ask

Can constant be overridden?

It sounds like these "constants" aren't very constant. But in any case, you can't override variables.

Can properties be overridden in C#?

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be virtual , abstract , or override .

How do you override a base class property?

you can override properties just like methods. That is, if the base method is virtual or abstract. You should use "new" instead of "override". "override" is used for "virtual" properties.


1 Answers

class A   CONST = 'A'    def initialize     puts self.class::CONST   end end  class B < A   CONST = 'B' end  A.new # => 'A' B.new # => 'B' 
like image 127
Konstantin Haase Avatar answered Nov 06 '22 14:11

Konstantin Haase