Blog |

How to Resolve NameError: Uninitialized Constant in Ruby

How to Resolve NameError: Uninitialized Constant in Ruby
Table of Contents

A NameError is raised when a referenced variable or a constant, such as a module, a class, or a constant variable, isn't defined or is invalid. The Uninitialized Constant error is a variation of the NameError exception class and has several reasons to occur.

NameError: uninitialized constant Object::Something

When this error occurs, several class names appear in place of Something.

What causes an Uninitialized Constant Error?

Let’s look at the most common reasons why NameError: Uninitialized Constant occurs:

  1. Whenever a class or module is referred to in code that can't be found, this error occurs. This often happens because the require method is missing, which is used to load another file and import all classes and method definitions from that file.
  2. Methods and variables in Ruby start with lowercase letters, whereas class names begin with uppercase letters. So in the code, if this distinction is missing, we'll get an Uninitialized Constant exception.
  3. Ruby is case-sensitive, so if there is any typo in the code, this error pops up. One small example of that is:
    X = 20;
    Y = 50;
    SUM = X+Y;
    print "Sum of x + y = ", sum;
    
    

    When the above code is executed, we get the following output:

    undefined local variable or method 'sum' for main:Object (NameError)
    
    
  4. If the code contains rubygems, which all new versions of Ruby disapprove of.

Example of NameError: Uninitialized Constant Class

Let’s examine and analyze one of the causes of Uninitialized Constant errors. This one occurs when the configuration of the class file containing the method being called is not loaded into the Rails console or the server when started. Let's say a file with the name Dummy.rb has been created in the lib folder of the Rails project.

class Dummy
    def self.get_Date
        Time.now.strftime('%F')
    end
end

Now, we start the Rails console and use the below command line to call the function get_Date:

 Dummy.get_Date

We get the following error:

NameError: uninitialized constant Dummy

Here, Dummy is the class name. This error shows that the class Dummy was not loaded when the Rails console was started.

How to Resolve NameError: Uninitialized Constant Class

To resolve this error, we have to load our class into the environment, and to do so, we have to add the following to the application.rb file of the Rails project:

config.autoload_paths += %W(#{config.root}/lib)

By adding this line, we are setting autoload_paths for the config file, because of which all the Ruby files that are present inside the lib folder will get automatically loaded whenever the Rails console or server is started.

When we execute the same Dummy.get_Date command in the console after adding the line, we get the correct output:

2022-12-25

Looking into the Uninitialized Constant error makes it clear that the majority of reasons may be resolved by carefully reviewing the code to check for typographical errors or by making modifications to the application.rb file's source code.

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Ruby on Rails errors easier than ever. Try it today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape