Posted by Andy on Wednesday, April 20, 2011.

Introducing Bombshell: custom interactive consoles for your Ruby libraries

One of the cool features of carbon, the Ruby library for our CM1 web service, is its built-in interactive console for experimenting with the service. At Brighter Planet we use carbon’s console all the time to construct one-off calculations and perform simple analyses.

We think the built-in library console could be a pretty useful pattern for other libraries; they allow developers to explore your library and its API without having to contrive host applications just to test things out.

Building carbon’s console, however, was a major pain. IRB, the console’s basis, is infamous for its poor documentation and, with all due respect, antiquated API. So we extracted all the little tricks and nuances that went into carbon’s console and put them in a library.

That library is Bombshell. To use it, just create a class for the console in your library and hook it into Bombshell. Instance methods on your class are “commands” in your console:

module FooGem
  class Shell < Bombshell::Environment
    include Bombshell::Shell
    prompt_with 'fooshell'
    def report
      puts FooGem::Foo.new.report! # Do something with your library here
    end
  end
end
fooshell> report
All is well!

Bombshell provides callback hooks, supports arbitrary-depth subshells, and handles tab-completion automatically.

Check out the README, the docs, and carbon’s shell class (and subshell) for a more complex (production) example.

Thanks to Aslak Hellesøy, whose aruba Cucumber steps made it possible to write a pretty thorough test suite for Bombshell.

What blog is this?

Safety in Numbers is Brighter Planet's blog about climate science, Ruby, Rails, data, transparency, and, well, us.

Who's behind this?

We're Brighter Planet, the world's leading computational sustainability platform.

Who's blogging here?

  1. Patti Prairie CEO