NameError: uninitialized constant

I recently opened up pry, ran this:

test = TestGem.new

but got this:

NameError: uninitialized constant TestGem

Not greatly helpful.

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

bundle

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 = GuessGemName.new
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.new
=> #<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
pry
gem_name.help
>
> # require with 
> require 'gem_name/gemname'
> # use with
> test = GemName.new

Ruby LoadError

Ever get this message?

/Users/me/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’: cannot load such file — <some_gem_i_need> (LoadError)
from /Users/me/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from <your_script>.rb:1:in `<main>’

You and Ruby may have different ideas exactly where that gem was installed.

For example, you could have used sudo when installing a gem and installed it at a path that Ruby wasn’t expecting or can’t access. Try using plain old ‘gem install’

How to make a Ruby script into a daemon process

Use Daemons.

1.Install

gem install daemons

2. Then, say you have myserver.rb, create a control script, myserver_control.rb, like this:

require 'rubygems'        # if you use RubyGems
require 'daemons'

Daemons.run('myserver.rb')

3. and use it from the console like this:

$ ruby myserver_control.rb start
    (myserver.rb is now running in the background)
$ ruby myserver_control.rb restart
    (...)
$ ruby myserver_control.rb stop

http://daemons.rubyforge.org/

 

Cinch – super simple IRC bots

Here’s an example of a simple IRC bot using Cinch:

require ‘cinch’

bot = Cinch::Bot.new do
configure do |c|
c.server = “irc.freenode.org
c.channels = [“#cinch-bots”]
end

on :message, “hello” do |m|
m.reply “Hello, #{m.user.nick}”
end
end

bot.start

Just install using:

gem install cinch

https://github.com/cinchrb/cinch#readme

Getting Started (in Markdown): https://github.com/cinchrb/cinch/blob/master/docs/getting_started.md

and the docs: http://rubydoc.info/gems/cinch/frames

Other IRC Bot frameworks: https://www.ruby-toolbox.com/categories/irc_bots