tags:

views:

47

answers:

1

Yesterday, I found the following code in RSpec(http://github.com/dchelimsky/rspec/blob/master/lib/spec/runner/option_parser.rb line2)

class OptionParser < ::OptionParser

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

+3  A: 

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
bjg
Thank you. Just let me confirm if my understanding is correct. Is OptionParser refer to OptionParser in the standard library called optparse in my example?
suzukimilanpaak
Exactly. In your example `::OptionParser` refers to the standard library class
bjg