Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

Yesterday, I found the following code in RSpec:

class OptionParser < ::OptionParser

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

like image 357
suzukimilanpaak Avatar asked Jul 21 '10 17:07

suzukimilanpaak


1 Answers

An runnable example might explain the idea best:

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

Produces when run:

In module M
At top level
like image 63
bjg Avatar answered Oct 23 '22 12:10

bjg