Alias: Enhance Your Ruby Console/irb Experience

aliasA couple of weeks ago we featured Gabriel Horner‘s Hirb framework for formatting irb output. I’ve recently been playing with another of his projects, Alias, which further enhances the Ruby Console experience (but it conceivably could be used in your Ruby programs too).

It’s already possible to set up aliases in your .irbrc file, but this can get confusing and it’s easy to run into conflicts. Alias takes a more structured, hash-based approach which (by default) lets you set up aliases for constants, instance methods or class methods. For example:

create_aliases :instance_method, "ActiveRecord::Base"=>{"update_attribute"=>'ua'}

You can store and retrieve your aliases in an easy-to-read YAML configuration file and it’s simple to have script/console (or irb) load them automatically from that file on start-up, by adding a small amount of code to your environment.rb (or .irbrc) file.

Extending Alias is fairly straightforward. Just subclass Alias::Creator and implement a few methods for mapping the aliases, checking their validity and generating the Ruby code to be evaluated.

For more details, check out the documentation and Gabriel’s recent blog post. You can install the gem from Github with:

sudo gem install cldwalker-alias -s

jslab.pngAlso.. Jumpstart Lab is offering workshops teaching Ruby for beginning female programmers (Ruby Jumpstart) on August 1st and 2nd, then beginning Rails (Rails Jumpstart) for everyone on August 15 & 16. Save 10% with code “rubyinside”!

Leave a Reply

Your email address will not be published. Required fields are marked *