昨天,我在RSpec中找到了以下代码:
class OptionParser < ::OptionParser
Run Code Online (Sandbox Code Playgroud)
这是做什么的?这有什么区别class OptionParser < NameSpace::OptionParser?
一个可运行的例子可能最好地解释这个想法:
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
Run Code Online (Sandbox Code Playgroud)
运行时产生:
In module M
At top level
Run Code Online (Sandbox Code Playgroud)