IRB: Let’s Bone Up On The Interactive Ruby Shell
The Interactive Ruby Shell, more commonly known as IRB, is one of Ruby's most popular features, especially with new developers. You can bash out a one-liner, try a method you've just learned about, or even build a small algorithm or two without going the whole way to writing a complete program.
I've not posted much about IRB before and it doesn't tend to get a lot of love generally, so while I've been digging through some of the best content I can find on IRB learning all about its internals and ways to get more out of it, I thought I should share:
Demystifying Irb's Commands - An awesome look at the hidden internals, special methods and features within IRB and the impetus for starting this post. Author Gabriel Horner looks at stuff like workspaces, subsessions, and IRB-specific loading methods.
What's Your Favorite IRB Trick? - A list of community-provided IRB tricks and tips on Stack Overflow. Of particular note is the first answer that covers the basic subsession commands (jobs, fg #, and kill #). There are also suggestions for easier benchmarking, predefined data, and using Wirble (mentioned below).
Utility Belt - A "grab-bag of tricks, tools, techniques, trifles, and toys for IRB." There's a ton of useful stuff in here for IRB power users and the feature list is longer than my arm: Amazon S3 support, "with" statement, history buffer, Pastie support, OS X clipboard support, external text editor support, auto-indentation, and more.
irb & script/console tips - A few useful IRB tips and resources from Thoughtbot's Dan Croak. Very practical and code driven. Dan advises loading pp (pretty printer) by default from your .irbrc file.
irb_rocket: An irb plugin - irb_rocket is an add-on for IRB that presents the result of your expression at the end of the current line in a "hash rocket" style.
Rails Console Tricks (screencast) - A screencast by Ryan Bates on the Rails console (a cousin of IRB) featuring a ton of useful tips, some of which are useful to IRB users generally.
Wirble: Tab-Completion and Syntax Coloring for irb - Wirble (homepage) is a Ruby library that adds a number of features to IRB. Of particular interest is its addition of syntax coloring. Install the wirble gem, add a few lines to your .irbrc file, and you get colored results in the data structures coming back from your IRB commands.
IRB documentation from the old Pickaxe - A lengthy page about IRB from the first edition of Programming Ruby (the Pickaxe). Not very up to date but sufficient for Ruby 1.8. Covers command line options, configuration files, and some of the special variables are commands available when running IRB.
Try Ruby - Not got Ruby installed or want to play in an IRB that's not your own? whytheluckystiff's Try Ruby is a great way to play with IRB through a Web browser. It includes an interactive tutorial, too.
May 14, 2009 at 6:08 pm
Yeeeeah! It's time IRB got some love. It is, after all, the way most people get introduced to Ruby.
May 14, 2009 at 11:07 pm
Try my Utility Belt fork. It has some added bits that you may like: http://github.com/akitaonrails/utility-belt/tree.
May 15, 2009 at 12:24 am
What's funny is, I paged down to the comments area to leave a comment about Akita's Utility Belt fork myself. First comment I see is the comment I was going to make.
I've not been great about maintaining Utility Belt at all. Tim Connor also has a Utility Belt fork which I believe is superior to mine. ;-) I think the biggest improvement is that both these forks support Linux better than mine does.
May 15, 2009 at 2:48 am
Just so people don't take it the wrong way. I just added minor bits. Giles utility belt is a great foundation to work in. I hope you have some more time to add even more :-)
May 15, 2009 at 4:51 am
Windows users of utility_belt or wirble need to install the gem "win32console" to get the irb coloring, or else any output will contain unevaluated color codes, which make it hard to read return values.
Just install the gem and require 'win32/console' in .irbrc. Unfortunately this causes issues when typing text anywhere but the end of the current line, but I can live with that. If anyone has a solution for this problem though I would be delighted to hear it.
May 21, 2009 at 12:23 pm
I have really liked irb fastri before:
http://eigenclass.org/hiki/irb+ri+completion
June 2, 2009 at 11:35 pm
Peter, glad to be an impetus for your irb explorations. I've written a second irb article in the same vein as the first which covers all of irb's configuration options: http://tagaholic.me/2009/05/29/exploring-how-to-configure-irb.html