You are here:About>Computing & Technology>Ruby> Learn Ruby> Interactive Ruby - Your Ruby Playground
About.comRuby
Newsletters & RSSEmail to a friendSubmit to Digg

Interactive Ruby - Your Ruby Playground

From Apply Now,
Your Guide to Ruby.
FREE Newsletter. Sign Up Now!
When you are new to Ruby, it is easy to overlook the Interactive Ruby shell, or irb. This powerful tool can be used for trying out single line commands while you are getting used to Ruby, or to run whole programs. Underneath the hood of irb is the Ruby interpreter that we looked at in an earlier article. Let's investigate that a little by running the following command (bolded).
ruby -h
Usage: ruby [switches] [--] [programfile] [arguments]
In addition to all of the options that print out (not shown), notice the "Usage" statement on the first line. If you are not familiar with the conventions used in usage statements, the brackets ([ ]) are used to notate optional arguments. This means we can run the Ruby interpreter without specifying any additional information. Try it out by running the command ruby.

Interesting. When you run ruby without arguments it waits for you to type additional information. Try the following example by typing in the bolded parts. Also note that ^D is the notation used for pressing the Ctrl and D keys together. This signals the end of your input to the interpreter.

ruby
a = 10
puts a + 20
^D
30
So the interesting thing is that you were able to type in Ruby statements and have them run when you ended the input. Let's try one more test by combining some of the Ruby interpreter command line options and a little Ruby code.
ruby -n -e 'puts eval($_)'
a = 10
10
puts a + 20
30
nil
This time we specified a few switches. The -n switch wraps the specified script in a "while gets()" loop. The -e allows us to specify a script. This script executes each line of code we type in and writes out the return value. If you are not familiar with Ruby the return value might be confusing. Every line of code returns some value even if it is nothing, or nil in Ruby speak. Running this code is similar to our prior example except now we see the results as we enter it, or interactively.
Interactive Ruby builds on this functionality as we will look at next. You start irb by typing irb at the command line, which should produce something similar to the following.
C:\ruby>irb
irb(main):001:0>
It doesn't look like much yet, but let's keep going. Type in the following command and press Enter to run it.
C:\ruby>irb
irb(main):001:0> puts 'Hello, Wurld!'
Hello, Wurld!
=> nil
irb(main):002:0>
Nice! Ruby ran the puts command which prints the string 'Hello, W...' Darn phonetics! Let's fix that. Press the up arrow once to fix my spelling error and run it again. So you can see that irb adds in editing capabilities.
You might be wondering what the 001:0 numbers before the > sign mean. The set of 3 numbers is the line number and the number after the colon (:) is the indent level, in other words, the number of open blocks you currently have. Notice in the following sample how the indent level increases when an if statement starts and decreases when it is closed with end.
irb(main):001:0> var = 1
=> 1
irb(main):002:0> if var == 1
irb(main):003:1>   var = 2
irb(main):004:1>   if var == 2
irb(main):005:2>     var = 3
irb(main):006:2>   end
irb(main):007:1> end
=> 3
irb(main):008:0>
Let's look at a final example to show-off Interactive Ruby subsessions. While you are running Interactive Ruby, you can type in irb again to start a subsession. Even better, you can specify an object to give that subsession a context to work in. An example will show why this is useful. Let's start with the following irb session.
irb(main):001:0> class Cat
irb(main):002:1>   def sound
irb(main):003:2>     'purr'
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> cat = Cat.new
=> #<Cat:0x35873c>
irb(main):007:0> puts cat.sound
purr
=> nil
At this point we have defined the Cat class and created an instance of Cat as the variable cat. We also called the Cat's method sound which gave a slight 'purr'. But what do I do if I want to redefine the sound method so it returns the more traditional 'meow'? I would have to type the whole Cat class back out! Enter the subsession.
irb(main):008:0> irb Cat
irb#1(Cat):001:0> def sound
irb#1(Cat):002:1>   'meow'
irb#1(Cat):003:1> end
=> nil
irb#1(Cat):004:0> irb_exit
=> #<IRB::Irb: @scanner=#<RubyLex:0x307a58>, @context=#<IRB::Context:0x307db4>, @signal_status=:IN_EVAL>
irb(main):009:0> puts cat.sound
meow
=> nil
By specifying the class Cat when I started the subsession, I was able to redefine the sound method. After exiting the subsession, my cat now meows. I also could have started the subsession with the cat object and only changed the sound my cat made and not all Cat's.

Hopefully this whets you appetite for learning more about Interactive Ruby. It is definitely worth the time to do so. One of the best places to start would be the Programming Ruby chapter on the Interative Ruby Shell.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.