Rails Console for Your Gem

Posted on 12 Dec 2012 by Eric Oestrich

I have been creating a gem recently and will usually want to open up an irb session to play around with ideas. When opening the irb session I had to configure the gem and it started to get annoying. I got envious of rails console and decided to make my own script/console for the gem.

Below is what it ended up looking like.

script/console
b = File.expand_path('../../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

require 'my_gem'
require 'irb'

MyGem.configure do |config|
  config.name = "My Gem"
end

IRB.setup(nil)
irb = IRB::Irb.new
IRB.conf[:MAIN_CONTEXT] = irb.context
irb.context.evaluate("require 'irb/completion'", 0)

trap("SIGINT") do
  irb.signal_handle
end
catch(:IRB_EXIT) do
  irb.eval_input
end
comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.