Datasheet

Little to nothing gets lost in translation. But the fact that the computer doesn’t
run your original code makes a difference in the way you think about the
development cycle. The immediate, hands-on feeling of an interpreted lan-
guage gives an extra lift to the agile development mindset.
Ruby is reflective
A Ruby program can reflect upon its own code, like a philosopher reflecting
on his or her own life. More specifically, a Ruby program can turn a string of
characters into executable code and can do this somersault during the run of
a program. Listing 1-1 contains an example:
Listing 1-1: Defining a Database Table
print “Enter some text: “
STDOUT.flush
text_input = gets
puts
print “You entered: “
print text_input
puts
print “Maybe you entered some Ruby code!\n”
print “I’ll try to execute the text that you entered.\n”
print “The result of executing your text is “
eval text_input
Figures 1-1 and 1-2 show two different runs of the code in Listing 1-1. In each
run the code prompts you to type some text. Ruby does two things with
whatever text you type:
Ruby echoes the text (displays the text a second time on the screen).
Ruby interprets your text as Ruby code and executes the code if possible.
The second step (reinterpreting text as code) is difficult to do in other pro-
gramming languages. Ruby makes it easy to reinterpret text as code, and this
ease makes life better for computer programmers.
Figure 1-1:
A run of the
code in
Listing 1-1.
16
Part I: Nuts and Bolts
05_081204 ch01.qxp 11/30/06 11:09 PM Page 16