1. Computing

The Interactive Ruby Prompt

By

The Interactive Ruby Prompt

One of the great things about Ruby is that it's test-driven. The interactive Ruby prompt provides an interface to the Ruby language for instant experimentation. This comes in handy while learning Ruby and experimenting with things like regular expressions. Ruby statements can be run and the output and return values can be examined immediately. If you make a mistake, you can go back and edit your previous Ruby statements to correct those mistakes.

To start the IRB prompt, open your command-line and run the irb command. You'll be presented with the following prompt:

irb(main):001:0>

Type the "hello world" statement we've been using throughout this series of tutorials into the prompt and hit Enter. You'll see any output the statement generated as well as the return value of the statement before being returned to the prompt. In this case, the statement output "Hello world!" and it returned nil.

irb(main):001:0> puts "Hello world!"
Hello world!
=> nil
irb(main):002:0>

To run this command again, simply press the up key on your keyboard to get to the statement you previously ran and press the Enter key. If you want to edit the statement before running it again, press the left and right arrow keys to move the cursor to the correct place in the statement. Make your edits and press Enter to run the new command. Pressing up or down additional times will allow you to examine more of statements you've run.

The interactive Ruby tool should be used throughout learning Ruby. When you learn about a new feature or just want to try something, start up the interactive Ruby prompt and try it. See what the statement returns, pass different parameters to it and just do some general experimenting. Trying something yourself and seeing what it does can be a lot more valuable then just reading about it!

  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. The Interactive Ruby Prompt

©2014 About.com. All rights reserved.