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’