Let's write a function to compute factorials. The
mathematical definition of n factorial is:
n! = 1 (when n==0) = n * (n-1)! (otherwise)
In ruby, this can be written as:
def fact(n) if n == 0 1 else n * fact(n-1) end end
You may notice the repeated occurrence of end. Ruby
has been called "Algol-like" because of this. (Actually, the
syntax of ruby more closely mimics that of a langage named
Eiffel.) You may also notice the lack of a return
statement. It is unneeded because a ruby function returns the
last thing that was evaluated in it. Use of a return
statement here is permissible but unnecessary.
Let's try out our factorial function. Adding one line of code gives us a working program. Click on the Try It! button to run the program. When the Zamplizer page opens up you will see that there is a 5 above the program. This is the command line argument that is passed to the program. You can change the 5 to another number and press the Run it! button to rerun the program and thereby compute the factorial of your number.
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
puts fact(ARGV[0].to_i) |
Output (when input is 5):
120
Here, ARGV is an array which contains the command line
arguments, and to_i converts a character string to an
integer.
Does it work with an argument of 40? It would make your calculator overflow...
See for yourself! Click on the above Try It! button, then on the Zamplizer page type 40 in the command line argument area in place of the number 5 and then clicking on the Run It! button.
It does work. Indeed, ruby can deal with any integer which is allowed by your machine's memory. So 400! can be calculated (output folded for readability):
We cannot check the correctness at a glance, but it must be right. :-)
The input/evaluation loop
When you invoke ruby with no arguments, it reads commands from standard input and executes them after the end of input:
The ^D above means control-D, a conventional way to signal end-of-input in a Unix context. In DOS/Windows, try pressing F6 or ^Z instead.
The input/evaluation loop does not apply when using Zamples. Each time you press the Run It! button in the Zamplizer you can modify the program or the command line arguments, and rerun the code example by clicking the Run It! button again:
puts "hello world" puts "good-bye world" |
eval.rb
Ruby also comes with a program called eval.rb that allows you to
enter ruby code from the keyboard in an interactive loop, showing you
the results as you go. It will be used extensively through the rest
of this guide when running code examples from the command line.
If you have an ANSI-compliant terminal (this is almost certainly true
if you are running some flavor of UNIX; under old versions of DOS you
need to have installed ANSI.SYS or ANSI.COM; Windows XP,
unfortunately, has now made this nearly impossible), you should use
this enhanced eval.rb that adds visual
indenting assistance, warning reports, and color highlighting.
Otherwise, look in the sample subdirectory of the ruby
distribution for the non-ANSI version that works on any terminal.
Here is a short eval.rb session:
hello world is produced by puts. The next
line, in this case nil, reports on whatever was last
evaluated; ruby does not distinguish between statements and
expressions, so evaluating a piece of code basically means
the same thing as executing it. Here, nil indicates
that puts does not return a meaningful value. Note
that we can leave this interpreter loop by saying exit,
although ^D still works too.
Throughout this guide, "ruby>" denotes the input prompt
for our useful little eval.rb program.
eval.rb and Zamples
eval.rb has been adapted for use with Zamples. To invoke it, simply pass eval.rb as the first argument on the command line. The token 'eval.rb' will be removed from the command line prior to executing the sample code. The colors have been altered for the Zamples version of eval.rb because the HTML page and the console have different background colors.
str="Hi, mom!" puts(str) |
Output:
ruby> str="Hi, mom!" "Hi, mom!" ruby> puts(str) Hi, mom! ruby> exit
It is often useful to 'single step' through the code with eval.rb on the command line. To just see the output without the single step information, simply erase eval.rb from the command line and rerun the code example from the Zamplizer.
str="Hi, mom!" puts(str) |
Output:
Hi, mom!
|
Copyright (c) 2005 Mark Slagell Portions copyright (c) 2005 Zamples, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License." |
![]() |
|


