NameError: uninitialized constant

I recently opened up pry, ran this:

test =

but got this:

NameError: uninitialized constant TestGem

Not greatly helpful.

It turned out that, despite adding it to my Gemfile and running


I still needed to require the file in pry.

However, the name of the file isn’t obvious if it’s not your own gem. E.g.

require 'guess_incorrect_gem_name'
LoadError: cannot load such file -- guess_incorrect_gem_name

Eventually it seemed to be correct:

require 'guess_gem_name'
=> true

but this didn’t work:

test =
NameError: uninitialized constant GuessGameName

but by looking at the gem file name via:

ls ~/.rvm/gems/ruby-2.1.5/gems/gem_name/lib/gem_name/gemname.rb

I eventually used:

require "gem_name/gemname"

which worked. E.g.

test =
=> #<GemName:0x007fc8aae926c0>

This all seems a lot of guess work.

What I’d like to see (and I’m sure there must be a way of doing this but Google’ing and searching StackOverflow has not turned anything up) is something like this:

gem install gem_name
> # require with 
> require 'gem_name/gemname'
> # use with
> test =

TDD with RSpec – Spec::Expectations and Expression Matchers

Spec::Expectations lets you set expectations on your object by adding 2 methods to Object. 

These are:

result.should == 42

result.should be_an_instance_of Book

Both methods take an optional Expression Matcher.
As well as defined Expression Matchers RSpec can create custom Matchers on the fly. A Ruby predicate is a method ending with a ?  and returns true or false. 

[].should be_empty => [].empty? #passes
[].should_not be_empty => [].empty? #fails
As well as be_ you can also use “be_a_” and “be_an_”. E.g. 
  3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
Note that as of 2.11 RSpec includes a new variant to this syntax.
i.e. the old syntax is:
foo.should eq(bar)
foo.should_not eq(bar)
and the new:
expect(foo).to eq(bar)
expect(foo).not_to eq(bar)
There’s a rather good post about this here:

Rails: routes

A brief breakdown of this route:

    match “/pick_gender(/:gender)”, to: “my_controller#pick_gender”, via: “GET”, as: :pick_gender 

1. `match` will match a GET (i.e. it will be the same as using `get`) if we use `via: “GET” 

You can simplify it to:

    get “/pick_gender(/:gender)”, to: “my_controller#pick_gender”, as: :pick_gender 

or if you want to use the `=>` syntax to:

    get “/pick_gender(/:gender)” => “my_controller#pick_gender”, as: :pick_gender 

2. `to:` matches this resource (the GET on the URL) to the `pick_gender` method of the `my_controller` controller 

3. finally, using `as:` will create a named route, i.e. you get a `pick_gender_path` and `pick_gender_url` for free as named helpers

More on named routes here:

More on Rails routes generally here:

More on `match` vs `get` here:

Rails with Passenger: Bundler::GemNotFound

This sometimes happens after a system update.

Run `bundle update` to fix.

In case this doesn’t work see also:


Ruby: interrupt a running script

Need to stop a running script in mid-stride and check/change variables?

Here’s how:

1. add Pry

require ‘pry’

2. add an Exception handler

rescue Exception => exception



Now just use Ctrl C (on a Mac, Unix and Linux) and you’ll exit out with a debugger command prompt.

And exit pry permanently with:



Ruby: require and require_relative; and why does Ruby remove “.” from the LOAD_PATH

require_relative lets you load a file relative to the file containing the require_relative statement.

The docs:

See also:

Ruby removes `.` from the LOAD_PATH for security reasons.

You can get around it by: 

1. using absolute paths

File.expand_path(__FILE__) etc…

2. using `./`, e.g.

require ‘./filename’

3. or require_relative

require_relative ‘filename’

Ruby: building a command-line application

Some great tips on building a command line application in Ruby:

1. Add a shebang line to your script

Rather than hard-coding the path to the Ruby executable in your script, use `env` to figure it out from your environment. E.g.

#!/usr/bin/env ruby

This makes your script easier to move between servers where the Ruby binary may be in different locations.

2. Make your script executable

chmod +x path/to/your/script

3. Add the path to your lookup path so that it can be called directly

export PATH=/Users/seacreature/devel/rcat/bin:$PATH

On a Mac you’d do this in your bash_profile.

Ruby: colon before or after a name?

You might see code like this:

hash = { :name => “David”, :age => 49 }

and then also see code like this:

hash = { name: “David”, age: 49 }

Why the difference in the position of the colon?

Quoting from “The Well Grounded Rubyist”:

“Ruby allows a special form of symbol representation in the hash-key position, with the colon after the symbol instead of before it and the hash separator arrow removed”