Ruby Programming/Overview


| Installing Ruby →

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


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[编辑]


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 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[编辑]


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.



   * 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)[编辑]



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 contains many advanced features.



  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日查阅.