1

I'm trying to build my first gem. Using Ryan Biggs' tutorial as my guide, I did the following:

1) Created the gem scaffolding:

$ bundle gem hello_world

2) Edited the lib/hello_world.rb file:

require "hello_world/version"

module HelloWorld
  def hi
    "Hello world!"
  end
end

3) Installed the gem via bundler:

$ cd hello_world
$ bundle install

At this point, if I run

$ bundle show hello_world

it shows

/Users/ykessler/gems/hello_world

so it looks like it installed.

But when I try to require the gem from irb:

require '/Users/ykessler/gems/hello_world'

it can't load it:

2.0.0-p195 :003 > require '/Users/ykessler/gems/hello_world' 
LoadError: cannot load such file -- /Users/ykessler/gems/hello_world    
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'   
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'   
from (irb):3    
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'

Where am I going wrong?

1 Answer 1

7

You need to run gem build hello_world.gemspec

Then to install it, you run gem install hello_world from the root of your gem project. That will install your local gem using the .gem file that we just created in your directory (not the gem from rubygems.org if it exists).

Now, if you run gem list, you should see it. You should now be able to require your gem and and access your library from other ruby code. All you have to write is require 'hello_world'. There is no need to type the full path. In fact, that's a bad idea.

This is all explained pretty clearly in the rubygems.org documentation (http://guides.rubygems.org/make-your-own-gem/). It's very clear, helpeful, and it's where I learned how to make my first gem.

3
  • did exactly as you said- It shows up in the gem list fine, but when I do require hello_world from idb, I get "NameError: undefined local variable or method `hello_world' for main:Object".
    – Yarin
    Commented Aug 17, 2013 at 21:00
  • Sorry, I forgot the quotes around hello world. Commented Aug 17, 2013 at 21:00
  • Ah NICE- yes, it loaded! Thanks (I'm gonna stick with the rubygems tut you linked to - the bundler one makes no sense...)
    – Yarin
    Commented Aug 17, 2013 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.