Let's write a function to calculate the factorial. The mathematical definition of the factorial is as follows:
N! = 1 (when n = 0)
= N * (n-1 )! (Other Cases)
In Ruby, you can write as follows:
Copy codeThe Code is as follows:
Def fact (n)
If n = 0
1
Else
N * fact (n-1)
End
End
You may find that end appears repeatedly. Because of this, Ruby is called the "Algol" language. (In fact, Ruby syntax is more like Eiffel ). at the same time, you may also find that this function lacks the return statement. here, return can be used, but it is not necessary, because a Ruby function will automatically return the final element assigned to it.
Let's try our factorial function. Adding a line of code will make it a workable program:
Copy codeThe Code is 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 containing command line parameters. to_ I converts the string to an integer.
% Ruby fact. rb 1
1
% Ruby fact. rb 5
120
Can it work when the parameter is 40? It will make your calculator overflow )...
% Ruby fact. rb 40
815915283247897734345611269596115894272000000000
It does. In fact, Ruby can process any integer allowed by your machine's memory. In fact, it is 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 at a glance whether it is correct, but I think it should be right .:-)
Input/evaluate Loop
When you start Ruby without parameters, Ruby will read the command from the standard input and execute it after the input is complete:
% Ruby
Print "hello world \ n"
Print "good-bye world \ n"
^ D
Hello world
Good-bye world
Ruby also contains a file named eval. the rb program allows you to input Ruby code in an interactive loop and display the result accordingly. this program will be widely used in subsequent tutorials.
If you have an ANSI-compliant (ANSI-compliant) Terminal (if you are running a UNIX variant, it usually works); in DOS, you need to install ANSI. SYS and ANSI. COM; then you can use this support for visual contraction, warning prompt, enhanced color High Brightness Display eval. rb. if not, try the non-ANSI version under the sample directory in the Ruby release version. It can run on all the consoles. this is a short eval. rb dialog:
% Ruby eval. rb
Ruby> print "Hello, world. \ n"
Hello, world.
Nil
Ruby> exit
Hello world is printed by print. the final calculated value of the next nil report. Ruby does not distinguish between syntax and expression. Therefore, it is one thing to calculate a piece of code basically to execute it. here, nil means that print does not return a meaningful value. note: exit or ^ D can be used to exit the interpreter of this loop.
In the entire tutorial, 'Ruby> 'indicates the command prompt of this very useful but small eval. rb program.