Fixing the libxml-ruby gem error: uninitialized constant XML (NameError)

I found the highly recommended libxml-ruby gem but was having a lot of trouble getting my sample code below to work

require 'libxml'
doc = XML::Document.file('')
root = doc.root
puts "Root element name: #{}"

I kept getting this error

uninitialized constant XML (NameError)

on line 2 where I used “XML::”

After combing through the internets for an answer, I found it.

Just change

require 'libxml'


require 'xml/libxml'

and everything should go swimmingly.

PS: The code above should simply output ‘feed’

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>