0

I recently had cause to use the nokogiri gem to parse html but while i going through their documentation, i came across this ruby syntax that i hadn't seen before

html_doc = Nokogiri::HTML('<html><body><h1>Mr. Belvedere Fan Club</h1></body></html>')
xml_doc  = Nokogiri::XML('<root><aliens><alien><name>Alf</name></alien></aliens></root>')

The part of interest for me is Nokogiri::HTML('...'). This looks very much like a method invocation but i know ruby method names cannot be in capital letters. So i looked through code files nokogiri gem and i came across the following definition

module Nokogiri
  class << self
     ###
     # Parse HTML.  Convenience method for Nokogiri::HTML::Document.parse
     def HTML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
          Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
     end
  end
 # more code
end

I tried reproducing the same code

module How
   class << self
      def DOESTHISWORK
         puts "In How Method"
      end
   end
end

How::DOESTHISWORK

But it keeps coming back with the error "uninitialized constant How::DOESTHISWORK (NameError)". I know it has to do with the method name starting in capitals but i just haven't been able to figure out how it works in nokogiri.

Anwuna
  • 1,077
  • 11
  • 18

2 Answers2

3

The difference is in the Nokogiri example the method is being called with parentheses and a parameter value which identifies it as a method call. Your DOESTHISWORK method takes no parameters but can be called with empty parentheses e.g.

irb(main):028:0> How::DOESTHISWORK()
In How Method
=> nil

If you add a parameter to your method that can also serve to identify it as a method like so:

irb(main):036:0> How::DOESTHISWORK 'some param'

Starting method names with a lowercase letter is good practice but isn't enforced. Something that begins with a capital letter is assumed to be a constant and will be looked up as such, this is why the parentheses or parameter is needed to indicate a method is being referred to. Another example:

irb(main):051:0> def Example
irb(main):052:1> puts "An example!"
irb(main):053:1> end
=> nil
irb(main):054:0> Example
NameError: uninitialized constant Example
    from (irb):54
    from /Users/mike/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>'
irb(main):055:0> Example()
An example!
=> nil
mikej
  • 65,295
  • 17
  • 152
  • 131
  • But how come this works, though ruby ensures methods names start either with a lower case letter or an underscore character? – Anwuna Mar 24 '15 at 23:13
  • @Anwuna I've added a bit more detail to the answer: short version: It's good practice, but not enforced, to start method names with a lower-case character. – mikej Mar 24 '15 at 23:21
  • 1
    Thanks. Up until now I thought the interpreter actually enforced it. Didn't know it was more of a convention. – Anwuna Mar 24 '15 at 23:26
0

I also found this post to be very helpful

What are the restrictions for method names in Ruby?

It's good practice, while not mandatory, to start the method name with a lower-case character, because names that start with capital letters are constants in Ruby. It's still possible to use a constant name for a method, but you won't be able to invoke it without parentheses, because the interpeter will look-up for the name as a constant

Community
  • 1
  • 1
Anwuna
  • 1,077
  • 11
  • 18