What is Ruby's double-colon `::`?

后端 未结 10 1993
南笙
南笙 2020-11-22 10:09

What is this double-colon ::? E.g. Foo::Bar.

I found a definition:

The :: is a unary operator that all

10条回答
  •  悲哀的现实
    2020-11-22 10:36

    This simple example illustrates it:

    MR_COUNT = 0        # constant defined on main Object class
    module Foo
      MR_COUNT = 0
      ::MR_COUNT = 1    # set global count to 1
      MR_COUNT = 2      # set local count to 2
    end
    
    puts MR_COUNT       # this is the global constant: 1
    puts Foo::MR_COUNT  # this is the local constant: 2
    

    Taken from http://www.tutorialspoint.com/ruby/ruby_operators.htm

提交回复
热议问题