Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the :: sign/operator before the class name in ruby?

in ruby, :: namespaces the module and class. But I often see :: at the beginning of the class name like the following:

#snippet of gollum gem
def page_class
  @page_class ||
    if superclass.respond_to?(:page_class)
      superclass.page_class
    else
      ::Gollum::Page
    end
end

What does that :: stands for if its in the beginning?

like image 347
Autodidact Avatar asked Sep 08 '10 09:09

Autodidact


1 Answers

It is to resolve against the global scope instead of the local.

class A
  def self.global? 
    true 
  end
end

module B

  class A
    def self.global?
     false
    end
  end

  def self.a
    puts A.global?
    puts ::A.global?

  end
end

B::a

prints

false
true
like image 73
einarmagnus Avatar answered Oct 02 '22 20:10

einarmagnus