Prev - DUP - Next - TOC

Simple examples


Now, we begin to start with a sample. First, let's consider a common example, the factorial function. The mathematical definition of the n factorial is:

  n! = 1                (n==0)
     = n * (n-1)!       (otherwise)

It is written in ruby as

 def fact(n)
   if n == 0
     1
   else
     n * fact(n-1)
   end
 end

You may notice many occurrences of `end'. Someone said `Algol-like' by the historical reason. Actually, the syntax of ruby is mimic the langage which is named Eiffel. You may also find lack of `return'. It works since ruby's `if' has its value. Of course, appending `return' is allowed, however, in case of without `return' it is faster rather than in case of returning by `return'.

Let's use this. Append the following line to the end of above program and save it into file, say `fact.rb'.

print fact(ARGV[0].to_i), "\n"

Here, `ARGV' is an array which contains command line argument, and `to_i' is method to convert to integer.

 % ruby fact.rb 4
 24

Does it work with argument of 40? It makes the calculator overflow...

 % ruby fact.rb 40
 815915283247897734345611269596115894272000000000

It worked. Indeed, ruby can deal with any integer which is allowed by your machines memory. So 400 can be obtained.

 % ruby fact.rb 400
 64034522846623895262347970319503005850702583026002959458684
 44594280239716918683143627847864746326467629435057503585681
 08482981628835174352289619886468029979373416541508381624264
 61942352307046244325015114448670890662773914918117331955996
 44070954967134529047702032243491121079759328079510154537266
 72516278778900093497637657103263503315339653498683868313393
 52024373788157786791506311858702618270169819740062983025308
 59129834616227230455833952075961150530223608681043329725519
 48526744322324386699484224042325998055516106359423769613992 
 31917134063858996537970147827206606320217379472010321356624 
 61380907794230459736069956759583609615871512991382228657857 
 95493616176544804532220078258184008484364155912294542753848 
 03558374518022675900061399560145595206127211192918105032491 
 00800000000000000000000000000000000000000000000000000000000 
 0000000000000000000000000000000000000000000

We cannot check the correctness with a glance, but it must be right :-)

When you invoke ruby without an argument ruby reads a script from standard input then executes them after the end of input.

 % ruby
 print "hello world\n"
 print "good-bye world\n"
 ^D
 hello world
 good-bye world

However, you may want to use it as a shell. Using the following program, you can execute line by line. It isn't necessary to understand this program.

 line = ''
 indent=0
 print "ruby> "
 while TRUE
   l = gets
   if not l
     break if line == ''
   else
     line = line + l
     if l =~ /,\s*$/
       print "ruby| "
       next
     end
     if l =~ /^\s*(class|module|def|if|case|while|for|begin)\b[^_]/
       indent += 1
     end
     if l =~ /^\s*end\b[^_]/
       indent -= 1
     end
     if l =~ /\{\s*(\|.*\|)?\s*$/
       indent += 1
     end
     if l =~ /^\s*\}/
       indent -= 1
     end
     if indent > 0
       print "ruby| "
       next
     end
   end
   begin
     print eval(line).inspect, "\n"
   rescue
     $! = 'exception raised' if not $!
     print "ERR: ", $!, "\n"
   end
   break if not l
   line = ''
   print "ruby> "
 end
 print "\n"

Save this to a file `eval.rb', and run it (Actually, it is contained in `sample' directory in the source of ruby.)

 % ruby eval.rb
 ruby> print "hello world\n"
 hello world
 nil
 ruby> ^D

`hello world' in the second line is output by `print' and next `nil' is returned value of `print'. `nil' means `void (meaningless) value', it is used if the return value is not used e.g. `print'.

Anyhow, this short program is useful. Throughout this guide, `ruby> ' denotes input for this program.
(There is a program named `rbc.rb' in the sample directory, which is more useful. Please try it.)


Prev - DUP - Next - TOC