dmy

IRB and the missing idiom

written by admin 2829 days ago. Last update at 2013-04-02 16:29:26. Categories devel, ruby.

I have recently turned my eye on using Ruby for small and not so small programms. Honestly, Ruby is really exciting and should probably also been listed as one of those things that brings the wow! back on your computer (the other being Apple and Google). Then there’s this book about Ruby, the so called Pick-Axe, wetting my appetite.

The Pick-Axe writes also about irb, the interactive ruby interpreter. So what you can do with irb is to fire it up and hack away some ruby code to give it a try. When you eventually shutdown irb your typed code is gone of course. The book shows lots of such examples.

That’s all fine and good except that using irb this way is bit unrealistic. Actually what I really want to do is to load my ruby project in irb and call methods, create objects, inspect variables etc — without running my project of course. But this is exactly what happens when doing something like

% irb -r 'foobar.rb'

So how to tell irb not to execute my ruby code? Unfortunatly the Pick-Axe is rather silent on this. Here’s how to do it:

if __FILE__ == $0
  # executed if not in irb
  ..
end