Permanent permission always requires the use of :: .
The method call is idiomatic and usually a period ( . ), But :: also legal. This is not true for the so-called modular methods, but for calling any method for any object:
class Foo def bar puts "hi" end end Foo.new::bar
This is not so much “syntactic sugar” as just an alternative syntax, for example, the ability to write if or case either with a new line, then new line, or simply then .
This is specifically allowed since Ruby allows methods with the same name as the constant, and sometimes it makes sense to think that they are the same element:
class Foo class Bar attr_accessor :x def initialize( x ) self.x = x end end def self.Bar( size ) Foo::Bar.new( size ) end end p Foo::Bar
You see this usually in Ruby in the Nokogiri library, which has (for example) Nokogiri::XML , as well as the Nokogiri.XML method. When creating an XML document, many people prefer to write
@doc = Nokogiri::XML( my_xml )
You also see this in the Sequel library, where you can also write:
class User < Sequel::Model
Again, we have a method (Sequel.Model) named just like a constant (Sequel :: Model) . The second line can also be written as
class User < Sequel.Model(DB[:regular_users])
& hellip, but it doesn’t look so good.
Phrogz
source share