一提到ruby大家就会条件反射似地想起rails,似乎ruby只有一个用途,就是rails。虽然我没写过rails,但我以为ruby语言本身的魅力远远超过rails这个第三方框架。这是一门值得细细品味的语言,与这门语言同样值得品味的还有一本叫《Metaprogramming Ruby》的书。下面是摘自此书的一段话。
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这门简洁而又华丽的语言。