简单示例

    简单示例

    Let’s write a function to compute factorials. The mathematical definition of factorial is:

    让我们来写一个计算阶乘的函数。n的阶乘在数学上的定义是:

    In ruby, this can be written as:

    Ruby中,我们可以这样写:

    1. def fact(n)
    2. if n == 0
    3. else
    4. n * fact(n-1)
    5. end
    6. 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.)

    你可能注意到了上面反复出现的end,正是由于这个原因,Ruby也被称为“类Algol”语言(事实上,Ruby的语法更接近模仿一门叫做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.

    你可能还注意到了没有return语句,因为它并不是必须要有的,Ruby中函数将自动返回最后一个求得的值。在这里使用指令是可以的,但是却是多余的。

    让我们试用一下我们的阶乘函数。给我们的程序添加一行:

    Here, ARGV is an array which contains the command line arguments, and to_i converts a character string to an integer.

    这里,ARGV是一个包含了命令行参数数组to_i则将一个字符转换成了整数。

    1. 1
    2. ruby fact.rb 5
    3. 120

    Does it work with an argument of 40? It would make your calculator overflow…

    如果参数是40它仍会正常工作么?嗯,它可能会让你的计算器溢出……

    It does work. Indeed, ruby can deal with any integer which is allowed by your machine’s memory. So 400! can be calculated:

    没问题,Ruby甚至还可以处理你机器内存所允许的任何整数,所以400的阶乘也可以被计算出来:

    1. 64034522846623895262347970319503005850702583026002959458684445942802397169186831436278478647463264676294350575035856810848298162883517435228961988646802997937341654150838162426461942352307046244325015114448670890662773914918117331955996440709549671345290477020322434911210797593280795101545372667251627877890009349763765710326350331533965349868386831339352024373788157786791506311858702618270169819740062983025308591298346162272304558339520759611505302236086810433297255194852674432232438669948422404232599805551610635942376961399231917134063858996537970147827206606320217379472010321356624613809077942304597360699567595836096158715129913822286578579549361617654480453222007825818400848436415591229454275384803558374518022675900061399560145595206127211192918105032491008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

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

    我们显然不能通过匆匆一瞥就能判断其是否正确,但它应该是没有问题的。:-)

    输入/执行 交互环境

    当你调用Ruby时没有传递参数,它将从标准输入设备读取命令,并在输入结束后执行输入的命令。

    ```% ruby
    puts “hello world”
    puts “good-bye world”
    ^D

    hello world is produced by puts. The next line, in this case , 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.

    hello world是由puts产生的。在这个例子中下一行是nil,它代表最后一次计算的结果;Ruby没有区分语句和表达式,因此对一段代码进行计算基本上等同于执行它。

    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.

    这里的nil表示puts没有返回一个有意义的值。注意,我们可以通过exit命令来离开这个解释器交互环境,或者也使用之前的Ctrl+D

    Throughout this guide, “ruby>“ denotes the input prompt for our useful little eval.rb program.

    在本指南中,当我们正在运行这个有用的程序时,就会有ruby>这个输入提示。


    下一章 字符串