Uninitialized constant (NameError) problem – how to include a class?

Here is my directory structure:

/features/ninja.feature
/features/step_definitions/ninja_steps.rb
/src/ninja.rb

When I run

cucumber

in the root of my project, I get an uninitialized string constant Ninja (NameError) error. I’ve determined it’s caused by this line in my ninja_steps.rb file:

@ninja = Ninja.new :belt_level => belt_level

In my ninja.rb file:

class Ninja
  def initialize (belt_level)
  end
end

Do I need to add some sort of require at the top of my ninja_steps.rb file, or what? I can’t seem to figure out how to do that so that it doesn’t bomb out.

  • error pushing to heroku; heroku is trying to install something related to sqlite3
  • Setting ruby hash .default to a list
  • How to get all user model with GraphQL
  • Ruby on Rails send_file, code in controller action running twice
  • Can I programmatically retrieve a gem's spec from the gem itself?
  • Sprockets::FileNotFound with ZURB Foundation
  • RVM throws gems directory permission error during bundle install / rake assets:precompile
  • Loading an image from a subdirectory in a gem
  • 2 Solutions collect form web for “Uninitialized constant (NameError) problem – how to include a class?”

    Did you try adding an include at the top of the ninja_steps? Something like

    require File.expand_path(File.dirname(__FILE__) + "/../../src/ninja")
    

    should do the trick. Otherwise, cucumber has no idea what a Ninja is. 🙂

    To load all those files in a slightly different way than Bill Turner suggests, taking a hint from projects like cucumber/aruba:

    https://github.com/cucumber/aruba/blob/master/features/support/env.rb

    # env.rb
    # add your src dir to the load path
    $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../src')
    require 'ninja'
    
    Ruby is the best programming language in the world - Ruby on Rails.