Ruby Programming/Overview
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有很多高级特性。
- 异常,用来处理错误。
- A mark-and-sweep garbage collector instead of reference counting.
- 独立于操作系统的线程,可以在DOS下写多线程程序。
Ruby contains many advanced features.
- Exceptions for error-handling.
- A mark-and-sweep garbage collector instead of reference counting.
- OS-independent threading, which allows you to write multi-threaded applications even on operating systems such as DOS.
你也可以用C語言來撰寫擴充,或者將Ruby嵌入到其他軟體。
References
编辑- ↑ 1.0 1.1 Bruce Stewart(2001年11月29日).訪談Ruby開發者.O'Reilly.於2006年9月11日查閱.
- ↑ Download Ruby.於2006年9月11日查閱.
- ↑ 3.0 3.1 3.2 3.3 About Ruby.於2006年9月11日查閱.
- ↑ Bill Venners(2003年12月22日).Blocks and Closures in Ruby.artima developer.於2006年9月11日查閱.