The easiest way to run Ruby interactively is simply to type ``ruby''
at the shell prompt.
% ruby
puts "Hello, world!"
^D
Hello, world!
|
Here we typed in the single
puts
expression and
an end of file character (which is control-D on our system).
This process works, but it's sort of painful if you make
a typo, and you can't really see what's going on as you type.
In the
sample
directory in the Ruby distribution you'll find
a script named ``
eval.rb
''. It goes one step better by showing us
the value of each expression as it is entered:
% cd sample
% ruby eval.rb
ruby> a = "Hello, world!"
"Hello, world!"
ruby> puts a
Hello, world!
nil
ruby> ^D
%
|
Here we can see the output from
puts
, and then the return value
from
puts
(which is
nil
).
That's all fine and well, except that multiline expressions do not
work, and you can't edit the line you're on, or go back and use
previous lines (as you might with command history in a shell).
For the next step up from
eval.rb
, we have
irb
---Interactive
Ruby.
irb
is a Ruby Shell, complete with command-line history,
line editing capabilities, and job control. It is quite configurable
and has many options, so much so that it has its own appendix beginning
on page 517. We recommend that you get familiar with
irb
so you can try some of our examples interactively.