- 注册时间
- 2004-8-19
- 最后登录
- 2008-2-23
⑥精研
流氓甲
- 积分
- 3451
|

楼主 |
发表于 2004-10-4 16:58:15
|
显示全部楼层
PS1:不用再去研究破解Scripts.rxdata了,因为RGSS完全支持load,require语句。
在脚本中输入:
load \"d:/sequh.rb\"
就可以加载D盘的sequh.rb文件了,同理:
require \"D:/sequh.rb\"
这里我输入的是绝对路径,绝对路径的表示方法是:
把DOS格式的路径名中的“\\”统统改为“/”即可
而相对路径的获得,需要一点儿办法:
因为在我的游戏目录下有game.exe文件,所以我们可以通过它来获得游戏目录,然后得到绝对目录,把我们的相对路径加到绝对目录后面,例
子:
load \"#{File.dirname(File.expand_path(\"Game.exe\"))}/scripts/sequh.rb\"
其中的File.dirname(File.expand_path(\"Game.exe\"))便是游戏目录的绝对路径。
PS2对 Ruby 变量、引号、数组、对象以及方法的介绍
Joshua D. Drake (jd@commandprompt.com)
项目经理,Command Prompt, Inc.
2001 年 7 月
这一四部分系列的第一篇是关于 Ruby 编程的介绍,由 Joshua Drake 执笔,他还撰写了 Linux Networking HOWTO、Linux PPP HOWTO 和
Linux Consultants HOWTO,同时还是定向开发 Linux 的公司 Command Prompt,Inc. 的共同创始人之一。那些了解如何使用其它诸如 Perl
之类的脚本语言的读者将会从本系列中获益最多。Joshua 从讨论变量、引号、数组、对象和方法开始探讨 Ruby。这一 Ruby 系列中的后几篇
文章将会涉及更多高级的课题,包括开发图形化应用程序以及和数据库一起使用 Ruby。
从介绍中可以知道,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,第二个对象是 STDIN。enterWorld 对象是用户定义的对象,而 STDIN 对
象(Standard Input 的缩写)是 Ruby 内建的。
这个示例中还有两种方法。第一种是 gets,第二种是 chop!。前面提到过,方法对对象进行专门处理。明确地说,方法将在对象中执行一个操
作。用 gets 方法,我们告诉 Ruby 去获取 STDIN。当 Ruby 看到与 STDIN 关联的 gets,它就会等待键盘输入和一个回车。简而言之,
STDIN.gets 就是等待用户输入一些内容然后敲 Enter 键。
第二种方法 chop! 用来对用户定义的对象 enterWorld 进行专门处理。chop! 方法告诉 enterWorld 将 enterWorld 对象关联的数据的换行符
和回车符截去。如果不使用 chop!(或者 chomp!),那么包含在先前代码上下文中的下面语句永远都不会为真。
if enterWorld == $salut[0]
因为没有使用 chop!,所以得出结果将为假,$salut[0] 实际上就等于 $salut[0]\\n。新行是由 STDIN 对象从 gets 方法接收的输入产生的。
使用回车将会在值末尾添加一个换行符。
结论
Ruby 是一种非常强大而且易于使用的语言。如果您是一个出身于 C++、 Perl 或 Python 的程序员,您会发现它们与 Ruby 有一些极为相似之
处(尤其是 Python 语言)。这个系列的下一篇文章将会基于这篇介绍讨论一些更高级的课题,比如使用 Roby 模块以及文件操作。
参考资料
关于更多的 Ruby 基础知识,包括对于 Ruby 创建者 Yukihiro Matsumoto 的采访,请参阅 developerWorks 上的 “Ruby: A new language”
。
请参阅官方的 Ruby Web 站点。
请访问 Ruby 园地。
在 Ruby 详细说明中学习更多有关 Ruby 的知识。
查找更多关于 Ruby for Win32 的内容。
请在 Ruby Central 驻足。
请查阅 Ruby 应用档案,这是用 Ruby 维护的。
请参阅 Ruby 功能的简单列表,其中包含关于 Ruby 优缺点的简短讨论(英文版和德文版)。
请参阅 FileWatcher.org 的最新 Ruby 文件更新情况。
请阅读面向 Windows 用户的 Ruby。
请访问 Joshua Drake 的 Web 站点 Command Prompt。
请在 developerWorks 浏览更多的 Linux 参考资料。
请在 developerWorks 浏览 更多的开放源代码参考资料。
关于作者
Joshua Drake 是 Command Prompt, Inc(一个定向开发 PostgreSQL 和 Linux 的公司)的共同创始人之一。他当前还撰写 Linux Networking
HOWTO、Linux PPP HOWTO 和 Linux Consultants HOWTO,他这段时间最要紧的项目是为 O\'Reilly 写一本新的 PostgreSQL 方面的书 —
Practical PostgreSQL。请通过 jd@commandprompt.com 与他联系。
|
|