「书籍纠错」Ruby Under a Microscope

Published on:

最近看了 Pat Shaughnessy的「Ruby Under a Microscope: An Illustrated Guide to Ruby Internals」一书,发现书中epub版P335,Note的一句话说的比较困扰:

Ruby implements extend in exactly the same way, except the included class becomes the superclass of the target class’s class, or metaclass. Thus, extend allows you to add class methods to a class.”

上面这段话中,描述了extend的内部实现行为的时候,使用了 the superclass of the target class’s class 这样的描述, 百思不得其解,因为Ruby中任何类的类都是Class,于是想,这肯定是作者写错了。

我给Pat发了封邮件,描述了我的困扰,并且声明做了如下修改:

[ Ruby implements extend in exactly the same way, except the included class becomes the superclass of the target class’s metaclass. Thus, extend allows you to add class methods to a class. ]

并附上了代码:

class A; end
a = A.new
module B
  def t
     puts 11
  end
end

a.extend B
A.extend B

A.singleton_class.ancestors  #=>   [ #<Class:A> , B, #<Class:Object>,  #<Class:BasicObject>,  Class, Module, Object, Kernel, BasicObject ]

a.singleton_class.ancestors  #=> [#<Class:#<A:0x007ffad2238668>>, B, A, Object,  Kernel, BasicObject ] 


今天得到了Pat的回复:

Hi Alex,

Thanks so much for the feedback - it's great to hear you like the book :)

Yes you're absolutely right. My choice of words (class's class) was confusing. Your version is simpler and easier to understand. Both are technically equivalent and correct I think.

pat


特此记录,给那些如果看此书有同样困扰的朋友。

Comments

comments powered by Disqus