Let's write a function that calculates the factorial. The mathematical definition of factorial is as follows:
N! = 1 (when n==0)
= n * (n-1)! (Other cases)
In Ruby, you can write this:
Copy Code code as follows:
def fact (N)
If n = 0
1
Else
n * Fact (N-1)
End
End
You may find that end is recurring, and because of this, Ruby is called the "class Algol" language. (In fact, Ruby's syntax is more like Eiffel). At the same time, you may also find that the function lacks a return statement. Here you can use return, but not necessarily, because a Ruby function automatically returns the element that it finally assigns.
Let's try our factorial function. Adding a line of code makes it a working program:
Copy Code code as follows:
# program to find the factorial of a number
# Save this as fact.rb
def fact (N)
If n = 0
1
Else
n * Fact (N-1)
End
End
print fact (argv[0].to_i), "\ n"
Here, argv is an array of command line arguments, To_i converts strings to integers.
% Ruby Fact.rb 1
1
% Ruby FACT.RB 5
120
Does it work when the parameter is 40? It will cause your calculator to overflow (overflow) ...
% Ruby FACT.RB 40
815915283247897734345611269596115894272000000000
It does work out. In fact, Ruby can handle any number of integers that your machine's memory allows. Actually, 400! You can also:
% Ruby FACT.RB 400
64034522846623895262347970319503005850702583026002959458684
44594280239716918683143627847864746326467629435057503585681
08482981628835174352289619886468029979373416541508381624264
61942352307046244325015114448670890662773914918117331955996
44070954967134529047702032243491121079759328079510154537266
72516278778900093497637657103263503315339653498683868313393
52024373788157786791506311858702618270169819740062983025308
59129834616227230455833952075961150530223608681043329725519
48526744322324386699484224042325998055516106359423769613992
31917134063858996537970147827206606320217379472010321356624
61380907794230459736069956759583609615871512991382228657857
95493616176544804532220078258184008484364155912294542753848
03558374518022675900061399560145595206127211192918105032491
00800000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000
We can't see if it's right, but I think it's right. :-)
Input/Evaluation Loop
When you start ruby with no parameters, Ruby reads the command from the standard input and executes it at the end of the input:
% Ruby
print "Hello world\n"
Print "Good-bye world\n"
^d
Hello World
Good-bye World
Ruby also includes a program called EVAL.RB that allows you to enter Ruby code in an interactive loop and display the results. This program will be used extensively in the later tutorials.
If you have an ANSI-compatible (ansi-compliant) terminal (if you are running a UNIX variant, it is generally true), you need to install Ansi.sys and ansi.com in DOS, and then you can use this to support visual indentation, warning prompts, Color high brightness display of the enhanced eval.rb. If you can't, try the non-ANSI version of the sample directory in Ruby's release, which runs on all consoles. This is a short eval.rb dialogue:
% Ruby Eval.rb
ruby> print "Hello, world.\n"
Hello, world.
Nil
Ruby> exit
Hello World is printed out by print. The next line of nil reports the last computed value; Ruby does not differentiate between syntax and expression, so it is one thing to compute a piece of code fundamentally and execute it. Here, nil means that print does not return a meaningful value. Note: You can leave the interpreter for this loop with exit or ^d.
Throughout the tutorial, ' ruby> ' means a command prompt for our very useful but very small eval.rb program.