Brighter Planet's blog
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.
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.