What does the entry / operator " :: " mean in the "Prawn :: Table" view? ( scope resolution operator )

  • require Prawn::Table will fall. Did you mean require 'prawn'; Prawn::Table require 'prawn'; Prawn::Table ? - D-side
  • I write require 'prawn' require 'prawn / table' - yuriy
  • The question you have is somewhat different, edit, put in order. - D-side
  • one
    In this case, it is the handling of the Table class of the Prawn module. The colon is used to refer to nested objects. - Alex Krass
  • in this case, the answer, I think, is received - the key word "submodules". By the code, something has already started to turn out - yuriy

1 answer 1

This is so-called. "scope resolution operator", operator for specifying an identifier scope

In your case, it is just getting the Table class defined inside the Prawn module. Although both of them could easily be at least classes, although modules, this would not affect the behavior of the operator.

He looks like . , the point that Ruby uses to invoke methods . But using the point you cannot get nested constants , whether they are nested modules, classes, or just some values ​​(Ruby, in general, to the bulb, modules and classes are also values):

 module Foo BAR = :bar # константа class Baz # класс-в-модуле end def self.qux # метод класса :qux end end Foo.BAR # !> undefined method `Baz' for Foo:Class Foo::BAR # => :bar Foo.Baz # !> undefined method `Baz' for Foo:Class Foo::Baz # => Foo::Baz 

However, both can be used to call the methods of the class itself:

 Foo.qux # => :qux Foo::qux # => :qux 

... and the point is primarily looking for exactly the methods:

 class Wut def self.Wat # НЕ ДЕЛАЙТЕ ТАК. Методы принято именовать в snake_case, НЕ с большой буквы :Wat end class Wat end end Wut::Wat # => Wut::Wat Wut.Wat # => :Wat 

... and the methods of individual objects, too, in theory (but do not do that):

 class Hi def hi :hi end end Hi.new::hi # => :hi 

There is another special use case :: "y" to indicate the "absolute path" to the class, ignoring the things of the same name in the nearest visible areas:

 class Ambi; end class Huge class Ambi; end puts Ambi # |> Huge::Ambi puts ::Ambi # |> Ambi end 

Ruby community style guide recommends using :: only to get constant values, not to call methods. An exception are calls to constructors of the form Nokogiri::HTML() , although in a good way these are ordinary methods.

  • I downloaded, the essence, I kind of understand, thank you .. and the examples are complicated for me right away - yuriy
  • @yuriy well, I tried to fully cover all use cases :: . I’m writing not only for you, so there’s more than what you need, but it can still be interesting for you, because it reveals the language of the device :) - D-side