不再年轻.NET

一个IT教师眼中的生活.NET、感悟.NET和技术.NET

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

从介绍中可以知道,Ruby 是一种纯粹的面向对象的脚本语言,它由日本的 Yukihiro Matsumoto 开发。设计这种语言主要是用来控制文本处理和系统管理任务。

正如下面的示例所示,任何曾经编写过如 Perl 或 PHP 等脚本语言的人都会熟悉 Ruby 语法。但是,Perl 或 PHP 需要使用分号作为一个行结束符,Ruby 与它们不同,它不需要行结束符。某些开发者起先可能会对此感到有些困惑,不过我发现这实际上加速了开发的过程。例如,我再也不会收到“Error missing ; on line x”的消息了。为了对 Ruby 世界有一个初步的了解,并阐述这个思想,请看一个经典的 Hello World 示例:


清单 1. Hello world
#!/usr/bin/ruby
            #
            # My first ruby program
            #
            print "Hello World\n"





回页首


变量

Ruby 语言中的变量非常容易处理。让我们扩展第一个示例,并引入一个改变输出的简单方法。


清单 2. 改变输出
#!/usr/bin/ruby
            #
            # My first ruby program
            #
            # Declare our salutation
            $salut = "Good Bye World\n"
            # Prepare statements
            print "Hello World\n"
            print $salut

使用 $salut 变量,我们可以随时轻易地改变第二个 print 语句的输出。您可以在程序的任何地方声明变量。


清单 3. 声明变量
#!/usr/bin/ruby
            #
            # My first ruby program
            #
            $salut = "Good Bye World\n"
            print "Hello World\n"
            print $salut
            #
            # Declare a new value for $salut.
            #
            $salut = "Oh, I am sorry. I didn't mean to warn
            you...\n"
            print "What do you mean Good Bye World?\n"
            print $salut

如果执行上述示例,将会输出以下内容:

Hello World
            Good Bye World
            What do you mean Good Bye World?
            Oh, I am sorry. I didn't mean to warn you...

正如您看到的那样,变量的重新赋值生效了。





回页首


单引号和双引号

像大多数语言一样,Ruby 区分双引号和单引号。在 Ruby 语言中,双引号表示 Ruby 将解释包含在引号内的任何值。例如:

print "Hello World\n"

Ruby 解释器将转义 \n 并打印一个换行符到 STDOUT(标准输出)。但是,如果我们要执行带单引号的同样语句,例如:

print 'Hello World\n'

Ruby 将输出如下内容:

Hello World\n

注意新的一行没有被打印。取而代之的是 Ruby 把整个语句看作了文字,这和 Perl 使用的功能一样。





回页首


单词算术

在 Ruby 语言中,所有字符串都可以使用操作符。这就意味着在某种意义上我们可以对单词进行算术运算。例如:

$salut = $salut * 3

将会产生如下结果:

Oh, I am sorry. I didn't mean to warn you...
            Oh, I am sorry. I didn't mean to warn you...
            Oh, I am sorry. I didn't mean to warn you...

$salut = $salut + " 10, 9, 8..."
            print $salut

将会生成如下结果:

Oh, I am sorry. I didn't mean to warn you...
            Oh, I am sorry. I didn't mean to warn you...
            Oh, I am sorry. I didn't mean to warn you...
            10, 9, 8...

注意到尽管变量打印了三次,但是 "10, 9, 8..." 的串联只在程序最后打印了一次。为什么会这样?

原因是我们只把 "10, 9, 8..." 添加到了变量的末尾。我们并没有要求变量将 "10, 9, 8..." 添加到每一行。另外,注意对变量的当前使用的继承性也很重要。

一旦给一个变量赋了值,除非再重新赋值,否则它就一直是静态的,正如示例中我们将 $salut"Hello World\n" 改为等于 "What do you mean Good Bye World? \n" 。但是,当变量进行乘法和串联操作时情况就不是这样了。您可以从上一个示例看出,对变量使用串联(+ 符号)将会导致这个变量继承它早先的赋值 加上添加到它的赋值上的字符串。在我们的示例中,添加的字符串是 "10, 9, 8..."。





回页首


数组

如果知道将多次对 $salut 变量赋值,您会怎样做呢?这在很多情况下都会发生。因此,您可以将所有的变量值放进一个数组里,而不是手动地给变量重新赋值。

数组允许对每个变量值进行分别的处理。请看如下示例:

$salut = ['Hello World','Good Bye World','What do you
            mean Good Bye World?']
            print $salut

运行上述代码得到的输出如下所示:

Hello WorldGood Bye WorldWhat do you mean Good Bye
            World?

显然,这不是我们想要的输出。没有间隔也没有换行。因此,我们可以标识希望显示数组的哪一部分,并使用先前解释的串联技术来更方便地提供易读的输出。

$salut = ['Hello World','Good Bye World','What do you
            mean Good Bye World?']
            print $salut[0] + "\n"
            print $salut[1] + "\n"
            print $salut[2] + "\n"

将会导致如下输出:

Hello World
            Good Bye World
            What do you mean Good Bye World?

仔细分析这些代码。如果回顾一下我们建立的数组:

$salut = ['Hello World','Good Bye World','What do you
            mean Good Bye World?']

我们告诉 Ruby 定义一个名为 salut 的变量,其值为:

$salut = 0 1 2
Hello World Good Bye World What do you mean Good Bye World?

每个值通过一个数字来被识别。数字通过数组中的数字位置来定义。位置总是从 0 开始,并从 0 开始递增。所以要打印数组中的第 2 个值,您要输入:

print $salut[1]

最容易忘记的是字段从 0而不是从 1开始。





回页首


条件 ― IF,ELSE

我们已经探讨了用 Ruby 语言显示数据的基础知识。现在来看 Ruby 编程中有关逻辑的初步知识。也就是说,让我们根据从程序员那里获得的结果来指示 Ruby 程序执行基本功能。我们还要根据 Ruby 程序从用户那里获得的结果看如何执行条件语句。在这些示例中将使用新的代码。


清单 4. 根据结果执行基本功能
$salut = ['Hello World','Good Bye World','What do you
            mean Good Bye World?']
            print "When you enter the world, what do you say? "
            while enterWorld = STDIN.gets
            enterWorld.chop!
            if enterWorld == $salut[0]
            print "\n" + "Yes. Hello World would be
            polite.\n"
            break
            else
            print "You say '", enterWorld, "'?!\n" + "You
            humans are so rude!\n"
            end
            end

上面的代码段引入了 Ruby 开发的几个新的方面,让我们浏览这些片段。

按部就班地浏览我们的示例

#!/usr/bin/ruby
            #
            # My first interactive ruby script
            #
            # Define our main variable with an array
            $salut = ['Hello World','Good Bye World','What do you
            mean Good Bye World?']
            # Print my first question
            print "When you enter the world, what do you say? "
            # Create a while loop with the object enterWorld and
            await data from
            # standard input. The while loop will make sure that
            ruby continues
            # to process the application until the program tells
            it to stop.
            while enterWorld = STDIN.gets
            # Make sure we use the chop method on the enterWorld
            object. The use
            # of the chop method will insure that we strip new
            lines and carriage
            # returns from our input.
            # You will notice that using the chop method has an
            extra
            # character. The ! allows the existing object to be
            modified
            # by the method. If you did not use the !, you would
            have to redeclare
            # the enterWorld object for the if condition to
            correctly occur.
            enterWorld.chop!
            # Begin the condition sequence. Basically, if
            enterworld equals
            # Hello World, which is 0, within the array, print
            # a new line. Then print, "Yes, Hello World would be
            polite." to the screen.
            if enterWorld == $salut[0]
            print "\n" + "Yes. Hello World would be
            polite.\n"
            # The break statement tells ruby to stop executing if
            the previous
            # condition is met. If we did not include this in our
            while loop,
            # the program would run continuously.
            break
            # The else statement is used as the secondary
            condition. In other
            # words, if the first condition is not met, please do
            the following.
            else
            print "You say '", enterWorld, "'?!\n" + "You
            humans are so rude!\n"
            break
            # The end statement is used to close a condition or
            loop. In our case,
            # it is being used to close both. We are first closing
            our if
            # condition statements and then stopping our while
            loop.
            end
            end





回页首


对象和方法

这个代码段中用到的一些技术和方法您可能是第一次见到。Ruby 是一种面向对象的编程(Object Oriented Programming,OOP)语言。使用 OOP 时,通常情况下程序员将调用诸如对象和方法之类的项目。 对象就象一个容器。它包含自己特定的变量和函数。 方法是一种被调用的东西,就像函数对对象进行专门处理一样。如果看一下先前的示例,我们就可以显示工作中的对象和方法。

while enterWorld = STDIN.gets
            enterWorld.chop!

这里我们有两个对象和两个方法的示例。第一个对象是 enterWorld ,第二个对象是 STDINenterWorld 对象是用户定义的对象,而 STDIN 对象(Standard Input 的缩写)是 Ruby 内建的。

这个示例中还有两种方法。第一种是 gets ,第二种是 chop! 。前面提到过,方法对对象进行专门处理。明确地说,方法将在对象中执行一个操作。用 gets 方法,我们告诉 Ruby 去获取 STDIN 。当 Ruby 看到与 STDIN 关联的 gets ,它就会等待键盘输入和一个回车。简而言之, STDIN.gets 就是等待用户输入一些内容然后敲 Enter 键。

第二种方法 chop! 用来对用户定义的对象 enterWorld 进行专门处理。 chop! 方法告诉 enterWorldenterWorld 对象关联的数据的换行符和回车符截去。如果不使用 chop! (或者 chomp! ),那么包含在先前代码上下文中的下面语句永远都不会为真。

if enterWorld == $salut[0]

因为没有使用 chop! ,所以得出结果将为假, $salut[0] 实际上就等于 $salut[0]\n 。新行是由 STDIN 对象从 gets 方法接收的输入产生的。使用回车将会在值末尾添加一个换行符。





回页首


结论

Ruby 是一种非常强大而且易于使用的语言。如果您是一个出身于 C++、 Perl 或 Python 的程序员,您会发现它们与 Ruby 有一些极为相似之处(尤其是 Python 语言)。这个系列的下一篇文章将会基于这篇介绍讨论一些更高级的课题,比如使用 Roby 模块以及文件操作。

posted on 2006-10-30 21:52  不再年轻.NET  阅读(743)  评论(1编辑  收藏  举报