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

后端 未结 10 1974
南笙
南笙 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

    0 讨论(0)
  • 2020-11-22 10:36

    Adding to previous answers, it is valid Ruby to use :: to access instance methods. All the following are valid:

    MyClass::new::instance_method
    MyClass::new.instance_method
    MyClass.new::instance_method
    MyClass.new.instance_method
    

    As per best practices I believe only the last one is recommended.

    0 讨论(0)
  • 2020-11-22 10:37

    :: Lets you access a constant, module, or class defined inside another class or module. It is used to provide namespaces so that method and class names don't conflict with other classes by different authors.

    When you see ActiveRecord::Base in Rails it means that Rails has something like

    module ActiveRecord
      class Base
      end
    end
    

    i.e. a class called Base inside a module ActiveRecord which is then referenced as ActiveRecord::Base (you can find this in the Rails source in activerecord-n.n.n/lib/active_record/base.rb)

    A common use of :: is to access constants defined in modules e.g.

    module Math
      PI = 3.141 # ...
    end
    
    puts Math::PI
    

    The :: operator does not allow you to bypass visibility of methods marked private or protected.

    0 讨论(0)
  • 2020-11-22 10:38

    What good is scope (private, protected) if you can just use :: to expose anything?

    In Ruby, everything is exposed and everything can be modified from anywhere else.

    If you're worried about the fact that classes can be changed from outside the "class definition", then Ruby probably isn't for you.

    On the other hand, if you're frustrated by Java's classes being locked down, then Ruby is probably what you're looking for.

    0 讨论(0)
  • 2020-11-22 10:43
    module Amimal
          module Herbivorous
                EATER="plants" 
          end
    end
    
    Amimal::Herbivorous::EATER => "plants"
    

    :: Is used to create a scope . In order to access Constant EATER from 2 modules we need to scope the modules to reach up to the constant

    0 讨论(0)
  • 2020-11-22 10:45

    :: is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup:

    module SomeModule
        module InnerModule
            class MyClass
                CONSTANT = 4
            end
        end
    end
    

    You could access CONSTANT from outside the module as SomeModule::InnerModule::MyClass::CONSTANT.

    It doesn't affect instance methods defined on a class, since you access those with a different syntax (the dot .).

    Relevant note: If you want to go back to the top-level namespace, do this: ::SomeModule – Benjamin Oakes

    0 讨论(0)
提交回复
热议问题