一提到ruby你們就會條件反射似地想起rails,彷佛ruby只有一個用途,就是rails。雖然我沒寫過rails,但我覺得ruby語言自己的魅力遠遠超過rails這個第三方框架。這是一門值得細細品味的語言,與這門語言一樣值得品味的還有一本叫《Metaprogramming Ruby》的書。下面是摘自此書的一段話。express
The Great Unified Theory 「The Ruby object model is a beautiful place,」
Bill notes, with a dreamy expression on his face. 「There are classes,
eigenclasses, and modules. There are instance methods, class methods,
and Singleton Methods.」 At first glance, it all looks very complex.
Look closer, and the complexity fades away. If you put eigenclasses
together with regular classes and modules, you end up with the seven
rules of the Ruby object model:
1. There is only one kind of object—be it a regular object or a module. 2. There is only one kind of module—be it a regular module, a
class, an eigenclass, or a proxy class.
3. There is only one kind of method, and it lives in a module—most often in a class.
4. Every object, classes included, has its own 「real class,」 be it a regular class or an eigenclass.
5. Every class has exactly one superclass, with the exception of BasicObject, which has none. This means you have a single ancestors
chain from any class up to BasicObject.
6. The superclass of the eigenclass of an object is the object’s class. The superclass of the eigenclass of a class is the eigenclass
of the class’s superclass. (Try repeating that three times, fast! Then
look back at Figure 4.5, on the preceding page, and it will all make
sense.)
7. When you call a method, Ruby goes 「right」 in the receiver’s real class and then 「up」 the ancestors chain. That’s all there is to know
about the way Ruby finds methods.ruby
當我讀到這段話的時候我簡直要瘋了。用七條原則就能夠對一門語言下定義,試問還有哪門語言作獲得(至少就我學過的語言來講尚未第二門)。簡潔自己就是一種美,ruby又在其簡潔的邏輯假設基礎上,衍生出複雜多變的特性來,簡直能夠用華麗來形容。其實不少龐雜的理論體系都是從幾條平淡無奇的假設演繹出來的。
我但願能循由這七條線索,與你們一塊兒品位ruby這門簡潔而又華麗的語言。框架