Skip to content

Instantly share code, notes, and snippets.

@cecyc
Last active May 15, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecyc/6edc9005f277acd3df2716defff5e863 to your computer and use it in GitHub Desktop.
Save cecyc/6edc9005f277acd3df2716defff5e863 to your computer and use it in GitHub Desktop.
Creating your own gem gotchas

Sometimes when you try to run your file, you may get an error because the file is not in your Ruby path. You can:

Run irb -I. to fix this but this is a pain. You could alias irb to irb -I....

You could also use bundle console to open irb with your gem code loaded (assuming that you _are_using bundler).

You could also...

  task :console do
    require 'irb'
    require 'mah_lib'
    ARGV.clear
    IRB.start
   end

But that is essentially what bundle console does. An alternate approach is to:

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'your-gem-name'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start

Set that as bin/console and it will run the right thing. This is what bundle generates when you bundle gem your-gem.

But probably you just want to bundle console. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment