0

I was looking at the Nokogiri source code, and found this:

module Nokogiri
  class << self
    def XML # some args
      # some code
    end
  end
  module XML
    # more code
  end
end

How come the names don't conflict? How does it know what I'm referring to when then using Nokogiri::XML? Is this best practice? If so, is this just a way to get default module entry-points as is the case here?

xml = Nokogiri::XML(open("www.foo.com/bar.xml"))
bluehallu
  • 10,205
  • 9
  • 44
  • 61

1 Answers1

2

They are really look very similar:

module Noko
  class << self
    def XML # some args
      'method'
    end
  end
  module XML
  end
end
Noko::XML #access to a module
=> Noko::XML
Noko.XML # access to class method
=> "method"
Noko::XML() #access to a class method too
=> "method"

So, we can say, that without any params this expression behaves like a module, with params - like a method.

More about scope resolution operator in Ruby you can see in this SO question.

Community
  • 1
  • 1
Ilya
  • 13,337
  • 5
  • 37
  • 53