← Notation conventions| Hello world →
When learning Ruby, you will often want to experiment with new features by writing short snippets of code. Instead of writing a lot of small text files, you can useirb, which is Ruby's interactive mode.
Runirb from your shell prompt.
$irb --simple-prompt>>
The>> prompt indicates thatirb is waiting for input. If you do not specify--simple-prompt, theirb prompt will be longer and include the line number. For example:
$irbirb(main):001:0>
A simpleirb session might look like this.
$irb --simple-prompt>>2+2=> 4>>5*5*5=> 125>>exit
These examples show the user's input inbold.irb uses=> to show you thereturn value of each line of code that you type in.
If you useCygwin'sBashshell onMicrosoft Windows, but are running the native Windows version of Ruby instead of Cygwin's version of Ruby, read this section.
To run the native version ofirb inside of Cygwin's Bash shell, runirb.bat.
By default, Cygwin's Bash shell runs inside of theWindows console, and the native Windows version ofirb.bat should work fine. However, if you run a Cygwin shell inside of Cygwin'srxvtterminal emulator, thenirb.bat will not run properly. You must either run your shell (andirb.bat) inside of the Windows console or install and run Cygwin's version of Ruby.
irb prints out the return value of each line that you enter. In contrast, an actual Ruby program only prints output when you call an output method such asputs.
For example:
$irb --simple-prompt>>x=3=> 3>>y=x*2=> 6>>z=y/6=> 1>>x=> 3>>exit
Helpfully,x=3 not only does an assignment, but also returns the value assigned tox, whichirb then prints out. However, this equivalent Ruby program prints nothing out. The variables get set, but the values are never printed out.
x=3y=x*2z=y/6x
If you want to print out the value of a variable in a Ruby program, use theputs method.
x=3puts x