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?
我希望B使用CONST = 'B'的定义,但我不知道怎么用。什么好主意吗?
Greetings
问候
Tom
汤姆
4 个解决方案
#1
71
class A
CONST = 'A'
def initialize
puts self.class::CONST
end
end
class B < A
CONST = 'B'
end
A.new # => 'A'
B.new # => 'B'
#2
2
Sorry I couldn't get the code formatting to work in a 'comment' only in an 'answer' but this is in response to akostadinov's question to Hendrik "how is this different from his [Konstantin's] answer?"
抱歉,我不能让代码格式只在'注释'中工作,而这是针对akostadinov问Hendrik的问题“这和他[Konstantin]的答案有什么不同?”
I'd guess Hendrik was trying to access the constant from methods in his inheriting class & that depends on if it's an instance or static method. It seems to behave as you'd expect in an instance method. But maybe or maybe not how you'd expect for a static method. Even if that's not what Hendrik meant, this may be worth noting:
我猜亨德里克试图从继承类的方法中访问常量&这取决于它是实例还是静态方法。它的行为似乎与您在实例方法中预期的一样。但是,也许你对静态方法的期望不是这样的。即使亨德里克不是这个意思,这也值得注意:
If you have the exact class definitions as Konstantin, but you add a method to class A like this:
如果您有确切的类定义如Konstantin,但您添加一个方法到类a,如:
def self.print_const
puts CONST
end
Then you get A both times:
然后你得到两个A:
A.print_const # prints A
B.print_const # prints A
However if you define the method in A by referencing the class:
但是,如果您通过引用类来定义方法A:
def self.print_const
puts self::CONST
end
Then you get:
然后你会得到:
A.print_const # prints A
B.print_const # prints B
#3
1
I had a few issues with the solution by Konstantin Haase. When accessing the constant in an instantiated object of the inheriting class, the parent constant was used.
我对Konstantin Haase的解决方案有一些疑问。当访问继承类的实例化对象中的常量时,使用父常量。
I had to explicitly refer to the class.
我必须明确地提到这个类。
self.class::CONST
cheers
干杯
#4
0
In case anyone finds this and is using module extension instead, just use
如果有人发现了这个,并且正在使用模块扩展,那么就使用。
self::CONST
自我:常量
#1
71
class A
CONST = 'A'
def initialize
puts self.class::CONST
end
end
class B < A
CONST = 'B'
end
A.new # => 'A'
B.new # => 'B'
#2
2
Sorry I couldn't get the code formatting to work in a 'comment' only in an 'answer' but this is in response to akostadinov's question to Hendrik "how is this different from his [Konstantin's] answer?"
抱歉,我不能让代码格式只在'注释'中工作,而这是针对akostadinov问Hendrik的问题“这和他[Konstantin]的答案有什么不同?”
I'd guess Hendrik was trying to access the constant from methods in his inheriting class & that depends on if it's an instance or static method. It seems to behave as you'd expect in an instance method. But maybe or maybe not how you'd expect for a static method. Even if that's not what Hendrik meant, this may be worth noting:
我猜亨德里克试图从继承类的方法中访问常量&这取决于它是实例还是静态方法。它的行为似乎与您在实例方法中预期的一样。但是,也许你对静态方法的期望不是这样的。即使亨德里克不是这个意思,这也值得注意:
If you have the exact class definitions as Konstantin, but you add a method to class A like this:
如果您有确切的类定义如Konstantin,但您添加一个方法到类a,如:
def self.print_const
puts CONST
end
Then you get A both times:
然后你得到两个A:
A.print_const # prints A
B.print_const # prints A
However if you define the method in A by referencing the class:
但是,如果您通过引用类来定义方法A:
def self.print_const
puts self::CONST
end
Then you get:
然后你会得到:
A.print_const # prints A
B.print_const # prints B
#3
1
I had a few issues with the solution by Konstantin Haase. When accessing the constant in an instantiated object of the inheriting class, the parent constant was used.
我对Konstantin Haase的解决方案有一些疑问。当访问继承类的实例化对象中的常量时,使用父常量。
I had to explicitly refer to the class.
我必须明确地提到这个类。
self.class::CONST
cheers
干杯
#4
0
In case anyone finds this and is using module extension instead, just use
如果有人发现了这个,并且正在使用模块扩展,那么就使用。
self::CONST
自我:常量