LoginSignup
17
5

More than 5 years have passed since last update.

rubyの::について

Posted at

Rubyのコロンを使用した書き方(「::」)について、分かりやすいと思ったコードを備忘録的に残しておきます。
コロンを使用した書き方では、メソッドの呼び出し以外に定数の呼び出しにも利用され、下記は定数の扱い例です。

sample.rb
A = "::A"
class Foo
    A = "Foo::A"
    class Bar
        A = "Foo::Bar::A"
        p A         # => "Foo::Bar::A"
        p Foo::A    # => "Foo::A"
        p ::A       # => "::A"
    end
end

上記サンプルコードは次のサイトから頂きました。
stack overflow

17
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
5