Skip to content

Instantly share code, notes, and snippets.

@pasela
Created December 5, 2012 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasela/4213863 to your computer and use it in GitHub Desktop.
Save pasela/4213863 to your computer and use it in GitHub Desktop.
[ruby] Accessing current class constants
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
@pasela
Copy link
Author

pasela commented Dec 5, 2012

そもそも定数は継承されない。
親クラスの定数を参照できるのは探索順がそうなってるだけ、という話でいいのかな。

[ruby-list:11222] Re: クラス定数の継承

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment