Created
December 5, 2012 08:35
-
-
Save pasela/4213863 to your computer and use it in GitHub Desktop.
[ruby] Accessing current class constants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
CONST = 'foo' | |
def put_const | |
puts [ | |
self.class.name, # (1) | |
CONST, # (2) | |
self.class::CONST # (3) | |
].join(' ') | |
end | |
end | |
class Bar < Foo | |
CONST = 'bar' | |
end | |
# (1) (2) (3) | |
Foo.new.put_const #=> Foo foo foo | |
Bar.new.put_const #=> Bar foo bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
そもそも定数は継承されない。
親クラスの定数を参照できるのは探索順がそうなってるだけ、という話でいいのかな。
[ruby-list:11222] Re: クラス定数の継承