| You are here: | About>Computing & Technology>Ruby> Learn Ruby> Interactive Ruby - Your Ruby Playground |
![]() | Ruby |
Interactive Ruby - Your Ruby PlaygroundWhen 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 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 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($_)' 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>irbIt 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 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 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 CatAt 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 CatBy 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. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our 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. |


