《Programming Ruby中文版》的原文摘录

  • When you design a class, you decide what internal state it has and also decide how that state is to appear on the outside (to users of your class). The internal state is held in instance variables. The external state is exposed through methods we’re calling attributes. And the other actions your class can perform are just regular methods. It really isn’t a crucially important distinction, but by calling the external state of an object its attributes, you’re helping clue people in to how they should view the class you’ve written. (查看原文)
    [已注销] 2012-08-18 16:49:45
    —— 引自第58页
  • Ruby gives you three levels of protection: • Public methods can be called by anyone—no access control is enforced. Methods are public by default (except for initialize, which is always private). • Protected methods can be invoked only by objects of the defining class and its sub- classes. Access is kept within the family. • Private methods cannot be called with an explicit receiver—the receiver is always the current object, also known as self. This means that private methods can be called only in the context of the current object; you can’t invoke another object’s private methods. The difference between “protected” and “private” is fairly subtle and is different in Ruby than in most common OO languages. If a method is protected, it may be called by any instance of the defining class or it... (查看原文)
    [已注销] 2012-08-18 17:58:15
    —— 引自第64页
  • Block: First, parameters to a block are now always local to a block, even if they have the same name as locals in the surrounding scope. (You’ll get a warning message if you run Ruby with the -w option.) (查看原文)
    [已注销] 2012-08-19 10:48:40
    —— 引自第76页
  • Second, you can now define block local variables by putting them after a semicolon in the block’s parameter list. So, in our sum-of-squares example, we should have indicated that the square variable was block-local by writing it as follows: (查看原文)
    [已注销] 2012-08-19 10:48:40
    —— 引自第76页
  • use braces for single-line blocks and do/end for multiline blocks. (查看原文)
    龙猫 2012-11-30 13:20:36
    —— 引自第46页
  • 方法是通过向对象发送消息来唤起调用的。消息包含方法名称以及方法可能需要的参数。当对象接收到一条消息时,它在自己的类中查找相应的方法。如果找到了,该方法会被执行。 (查看原文)
    ziyoudefeng 2012-12-13 20:26:27
    —— 引自第12页
  • "protected"和"private"之间的区别很微妙,并且和其他大多数普通的面向对象语言都不同。如果方法是保护的,它可以被定义了该方法的类或其子类的实例所调用。如果方法是私有的,它只能在当前对象的上下文中被调用------不可能直接访问其他对象的私有方法,即便它与调用者都属同一个类的对象。 (查看原文)
    ziyoudefeng 2012-12-13 23:31:03
    —— 引自第37页
  • The difference between “protected” and “private” is fairly subtle and is different in Ruby than in most common OO languages. If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller. (查看原文)
    ziyoudefeng 2012-12-13 23:31:03
    —— 引自第37页