ruby ruby Colon访问操作员用例子解释

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby ruby Colon访问操作员用例子解释相关的知识,希望对你有一定的参考价值。

# Double Colon (::) is namespace resolution. It can also call Class Level Constants. So when you do this:

MyModule::MyClass::MySubClass
MyModule::MyClass::MySubClass::CONSTANT

# it is calling the name space of:
module MyModule
    class MyClass
        class MySubClass
            CONSTANT = 'test'
        end
    end
end

# This differs from Dot Operators (.) in that with Dot you are calling a property or method.
# This will call a method
MyModule::MyClass::MySubClass.new
MyModule::MyClass::MySubClass.run
MyModule::MyClass::MySubClass.property = test
MyModule::MyClass::MySubClass.property #=> test

#This either initializes the object or calls a class method:
module MyModule
    class MyClass
        class MySubClass
            CONSTANT = 'test'
            attr_accessor :property
            def initialize
                # stuff
            end
            def property=(property)
                @property = property
            end
            def property
                @property
            end
            def self.run
                # more stuff
            end
        end
    end
end

以上是关于ruby ruby Colon访问操作员用例子解释的主要内容,如果未能解决你的问题,请参考以下文章

[汇]Ruby知识点

通过 SWIG 从 Ruby 调用 Boost?

如何从 C 级代码访问 Ruby AST?

Ruby高级编程面向对象

Ruby - 控制器(或模型)中的访问参数

ruby Enumerator::lazy