Ruby is designed to read a program in one pass; this means you can
pipe an entire program to Ruby's standard input and it will work just
fine.
We can take advantage of this feature to run Ruby code from inside an
editor. In Emacs, for instance, you can select a region of Ruby text
and use the command
Meta-|
to execute Ruby. The Ruby interpreter
will use the selected region as standard input and output will go to a
buffer named ``
*Shell Command Output*
.'' This feature has come in
quite handy for us while writing this book---just select a few lines
of Ruby in the middle of a paragraph and try it out!
You can do something similar in the vi editor using ``
:!ruby
''
which
replaces the program text with its output, or
``
:w!ruby
'', which displays the output without
affecting the buffer. Other editors have similar features.
While we are on the subject, this would probably be a good place to
mention that there is a Ruby mode for Emacs included in the
distribution as
misc/ruby-mode.el
. There are also several
syntax-highlighting modules for vim (an enhanced version of the vi
editor), jed, and other editors available on the net as well. Check
the Ruby FAQ for current locations and availability.