Ruby simple example

Source: Internet
Author: User

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:

CopyCode The 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 workableProgram:

Copy code The 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.