Fresh Ruby Enhanced SHell

We love Ruby. And we love the command line. So… the shell needs to be rubyfied ;).

How does it work?

Basically, fresh is a Ruby console like irb: Enter a Ruby expression and it gets evaluated. But not everything is interpreted as Ruby: The input is thrown against a regular expression to determine if it is meant to be a Ruby or a system command.

May sound like voodoo, but works surprisingly well in practice ;).

Get fresh

Install the gem with:

gem install ripl-fresh

Start it with:

ripl fresh

(or just fresh)

Usage & configuration options

For an example session, see this blog entry.

The main regexp to determine if the command should be interpreted as system command is similar to this one: /^[a-z_-]\s.*/i (match a single word followed by at least one space). It can be adjusted in Ripl.config[:fresh_system_regexp].

Of course, there are also exceptions that should still get interpreted as Ruby (e.g. def ). These can be found and modified in the Ripl.config[:fresh_ruby_words] array.

Single words get interpreted as Ruby by default. You can set your “single system words” in Ripl.config[:fresh_system_words] (e.g. ls).

There is also a third kind of command mode (besides :ruby and :system): :mixed. They look and feel like system commands, but redirect to the Ruby method described by the first word. You can register them in Ripl.config[:fresh_mixed_words] (e.g. cd).

Of course, there is a way to explicitly set your command mode: You can prefix your input with a space to force Ruby mode as well as you can prefix it with ^ to force system mode. The strings used for this can be customized in Ripl.config[:fresh_system_prefix] and Ripl.config[:fresh_ruby_prefix].

You need to take a look at get_input method in the source file to 100% understand the command mode detection way.

Defaults

# prefixes
Ripl.config[:fresh_system_prefix] = %w[^]
Ripl.config[:fresh_ruby_prefix]   = [' ']
# single words
Ripl.config[:fresh_system_words]  = %w[top ls] 
Ripl.config[:fresh_ruby_words]    = %w[begin case class def for if module undef unless until while puts warn print p pp ap raise fail loop require load lambda proc system]
# catch mix words
Ripl.config[:fresh_mixed_words]   = %w[cd] 
# main regexp
Ripl.config[:fresh_system_regexp] = /^([a-z_-]+)\s+(?!(?:[=%*]|!=|\+=|-=|\/=))/i

Customization

Besides customizing your fresh with the configuration options, you can further enhance it with Ruby plugins, because it’s based on ripl. Just install the ripl-plugin_name and add it to your .riplrc file:

require 'ripl/plugin_name'

Currently, most plugins enable colors and IRB -like features.

TODO

There are lots of things which can get better:

  • Improve auto-completion

  • More cool (and colorful?) :mixed Ruby commands

  • Improve interaction between system and ruby commands

    • ripl-multi_line for system commands

    • Result of system commands should be available for ruby, not only printed to stdout

  • Improve default configuration

  • Fresh ideas

Feel free to fork in your improvements ;)

Other gems you might find interesting

  • rush - Another Ruby shell, different concept

  • ripl - An irb alternative, fresh is based on it

Copyright © 2010 Jan Lelis <code-needs-smileys.com> released under the MIT license.

J-_-L