Ruby NameError: uninitialized constant error message

Problem: When working with some Ruby code in an irb session, I just got the following error message:

NameError: uninitialized constant Tempfile

Here's a snippet of my irb session where this error occurred:

>> tmp = Tempfile.new
NameError: uninitialized constant Tempfile
	from (irb):1

Solution

This "NameError: uninitialized constant" looks intimidating, but it's actually not a big deal: I just forgot to require the tempfile package.

Here's the output of my irb session after I issued the proper require command:

>> require 'tempfile'
=> true

>> tmp = Tempfile.new('fred')
=> #<File:/var/folders/h5/h59HESVvEmG+3I4Q8lOAxE+++TI/-Tmp-/fred.1637.0>

# note that there was no error message this time.

>> exit

If you're getting a similar error message when running your Ruby code, just make sure you've included the class or package that you're trying to use.