What does the ClassName <:: OtherClassName class do in Ruby?

Yesterday I found the following code in RSpec :

 class OptionParser < ::OptionParser 

What does it do? What is the difference between this and class OptionParser < NameSpace::OptionParser ?

+6
source share
1 answer

A simple example might explain a better idea:

 class C def initialize puts "At top level" end end module M class C def initialize puts "In module M" end end class P < C def initialize super end end class Q < ::C def initialize super end end end M::P.new M::Q.new 

It is produced at startup:

 In module M At top level 
+8
source

All Articles