Ruby Programming/Overview

| Installing Ruby →

Ruby是个个体导向(object-oriented) (注:大陆地区译为“面向对象”;台湾地区早期译为“个体导向”或“物件导向”。配合类别的语意,本条目多采“个体”一词) 脚本语言(scripting language),开发者叫做松本行弘 简称 "Matz"。 Ruby的官方网站网址是http://www.ruby-lang.org。1993年二月开始著手开发,alpha版释出在1994年十二月。他有点像是Perl and Python[1]

Ruby大量借鉴了Perl,Ruby的类库其实是按照个体导向的原则,对Perl的函数库进行重组。Ruby也借鉴了Lisp和Smalltalk。Ruby没有从Python借鉴太多特性,但阅读Python有助于Matz开发Ruby。

Ruby borrows heavily from Perl and the class library is essentially an object-oriented reorganization of Perl's functionality. Ruby also borrows from Lisp and Smalltalk. While Ruby does not borrow many features from Python, reading the code for Python helped Matz develop Ruby.[1]

Mac OS X自带Ruby。常见的GNU/Linux发行版即使不自带Ruby,也能从其软件库中方便安装。Windows环境下也可以下载并安装Ruby。专家可以下载Ruby源码并编译,此方法适用Unix, DOS, BeOS, OS/2, Windows, 与 Linux。

Mac OS X comes with Ruby already installed. Most Linux distributions either come with Ruby preinstalled or allow you to easily install Ruby from the distribution's repository of free software. You can also download and install Ruby on Windows. The more technically adept can download the Ruby source code[2] and compile it for most operating systems, including Unix, DOS, BeOS, OS/2, Windows, and Linux.[3]

特性 Features 编辑

Ruby集合了Perl, Smalltalk, Eiffel, Ada, Lisp, 和Python的特性。

Ruby combines features from Perl, Smalltalk, Eiffel, Ada, Lisp, and Python.[3]

个体和混入 Objects and mixins 编辑

不像Java和C++,Ruby是个纯个体导向语言。所有事物都是个体,包括数字和基本类型。个体的属性叫实例变量,与个体绑定的函数叫方法。

Unlike Java and C++, Ruby is a pure object-oriented language. Everything is an object, including numbers and other primitive types. An object's properties are called instance variables and the functions associated with an object are called its methods.[3]

Ruby特意只支持单继承。Ruby程序员可以混入一个模块替代多继承,以得到模块的所有方法。和Objectiv-C的Categories特性差不多。Ruby程序员能发现,混入比多继承更方便,更强大。

Ruby intentionally only allows single inheritance. Instead of multiple inheritance, Ruby programmers can mixin a module to receive all of its methods, similar to the Categories feature in Objective-C. Ruby programmers often find mixins to be simpler and more powerful than multiple inheritance.[3]

弹性 Flexibility 编辑

在Ruby中,所用东西都是可变的。不用通过子类别,就可以给已存在的类别添加方法;运算子可以多载;甚至标准库也能在执行时重新定义。

In Ruby, everything is malleable. Methods can be added to existing classes without subclassing, operators can be overloaded, and even the behavior of the standard library can be redefined at runtime.

变数和作用域 编辑

Ruby不需要声明变数和变数的作用域。变数名指出其作用域。

   * x 本地變數(or something besides a variable)
   * $x 全局變數
   * @x 实例變數
   * @@x 類別變數

You do not need to declare variables or variable scope in Ruby. The name of the variable automatically determines its scope.

  • x is local variable (or something besides a variable)
  • $x is a global variable
  • @x is an instance variable
  • @@x is a class variable

块(闭包) Blocks (closures) 编辑

块,也就是闭包,是Ruby的强大特性。类似于Java的匿名类,但更简单。

闭包使你可以把一块代码传给一个方法。常用的例子是,给排序方法一个闭包,用闭包来比较两个值;闭包决定元素的排序方式。既可以按字母排序,也可按数字排序,也可以复杂排序。这都由闭包决定。例如,原来按产品ID排,闭包可以由产品ID查到期产品名称,在比较其名称,然后使用产品名称排序。

Blocks, also referred to as closures, are one of Ruby's most powerful features.[4] They are similar to Java's anonymous classes but are easier to use.

Closures allow you to pass a block of code to a method. A common example is to call a sort method and to pass (or attach) a closure that compares two values -- this closure determines how the items are sorted. The closure might compare the values alphabetically or numerically. The closure might also do something complicated. If the values being sorted are product IDs, the closure could retrieve product names from a database and then compare the product names instead of the product IDs.

高级特性 Advanced features 编辑

Ruby有很多高级特性。

Ruby contains many advanced features.

你也可以用C语言来撰写扩充,或者将Ruby嵌入到其他软体。

References 编辑

  1. 1.0 1.1 Bruce Stewart(2001年11月29日).访谈Ruby开发者.O'Reilly.于2006年9月11日查阅.
  2. Download Ruby.于2006年9月11日查阅.
  3. 3.0 3.1 3.2 3.3 About Ruby.于2006年9月11日查阅.
  4. Bill Venners(2003年12月22日).Blocks and Closures in Ruby.artima developer.于2006年9月11日查阅.