开发者

What does :: (double colon) mean in Ruby? [duplicate]

开发者 https://www.devze.com 2022-12-20 03:59 出处:网络
This question already has 开发者_JAVA百科answers here: What is Ruby's double-colon `::`? (12 answers)
This question already has 开发者_JAVA百科answers here: What is Ruby's double-colon `::`? (12 answers) Closed 8 years ago.

What does :: mean in Ruby? E.g. Foo::Bar.


From the Pickaxe:

When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period (.) or two colons (::). The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses.


It's called a scope resolution operator. Basically a fancy way of referencing a class within a namespace. ActiveRecord is the namespace and Base is the class.


It accesses constants in a given class or module. E.g. ActiveRecord::Base is the constant Base defined in the module ActiveRecord.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号