Tuesday, 4 March 2008

uninitialized constant RedCloth (NameError)

OK, here is another bone headed mistake I made that I will post about in hopes of helping other poor souls who run across the problem. This morning I decided to play around a bit with the RedCloth library that comes with Ruby 1.8 on Mac OS X 10.5 Leopard. So I wrote a simple program in Ruby and tried to run it. Every time I did I got:

uninitialized constant RedCloth (NameError)



And here is my program if you are interested:




#!/usr/bin/ruby
require 'rubygems'
require 'RedCloth'
doc = RedCloth.new "h2. test header

just some text

another para"
puts doc.to_html



Pretty simple. (RedCloth, by the way, is a processor for Textile. Kind of a simple yet powerful way to mark up text like you would in a wiki.) Well I know the RedCloth Gem is in my path and is installed. I know my code should work, but it doesn't. What is the problem?



Turns out I unfortunately named my test file "redcloth.rb". Turns out the Gem file is named "RedCloth.rb" and is in the path and can be executed from the command line. If you don't know, OS X is case insensitive like Windows XP, so those names are equivalent. Thusly, Ruby faithfully tried to load my local script file, which doesn't define any of the needed objects. Therefore, the error. Naming the file "rctest.rb" fixes the problem.



So I hope this little note helps someone else out in the future. So if you get a: "uninitialized constant XXXXXX (NameError)", make sure your file name isn't the same as a the Gem file.

No comments:

Post a Comment