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

1
2
3
4
require 'libxml'
doc = XML::Document.file('http://search.twitter.com/search.atom?q=obama')
root = doc.root
puts "Root element name: #{root.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'

to

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>