10

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?

1 Answer 1

9

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 
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Exactly. In your example ::OptionParser refers to the standard library class
Your example is good in terms of "runnability" but too scarce for a newbie :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.