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.


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