书籍 Metaprogramming Ruby 第二版草稿已经完成一半

scriptfans · 2013年12月14日 · 最后由 zhang_soledad 回复于 2015年04月29日 · 4306 次阅读

半个月前收到出版社提供的审校稿,保罗已经完成了 part1,内容针对 Ruby 2/2.1 做了调整和更新,一如既往的有料:)各位 Rubyist 对这本书有啥建议和意见的话,请发送给我,统一汇总提供给 Prog 社。email:chen.ruijie#gmail.com

支持。

如果能先睹为快的话... 是提不出意见的呀!

额,等我咨询下外方出版社可否公开第二版的草稿(目前仅完成了第一部分,建议等全本出来再说)……我自己倒是给保罗提了点建议,顺便贡献了两个 spell。

Page 22, The Paths of Constants.

In ruby 2.0, Module#const_get method can accepts qualfied constant strings, For example: Object.const_get("X::Y::Z").

Page 94, Method Objects

About method object, Module#define_method now accepts an UnboundMethod from a Module, In some scenarios, it is very useful:

module M
  def foo; "foo"; end
end
define_method :foo, M.instance_method(:foo)

Page 130, Around Aliases

On page 131 Paolo said: 'Ruby also provides Module#alias_method, a method equivalent to alias.' But there has some subtle difference between them, for details, please read this article: Alias vs Alias_method

Page 151, Quiz Solution

On page 152, Paul describes two methods that manipulate instance variables: Object#instance_variable_get and Object#instance_variable_set. But there is another method not mentioned: Object#remove_instance_variable. In ruby 2.0, Using this method you can now dynamically remove instance variables from an object.

TracePoint

Please consider adding introduction about this new feature, for meta-programming, I think this is a powerful tool: http://www.ruby-doc.org/core-2.0.0/TracePoint.html

I would like to contribute two spell :)

1 method self reference

_callee_ is a special method that returns the name of the currently called method. In Ruby 1.9, _callee_ returned the name of the current method from the context of the method, so even if it had been aliased or dynamically defined, you'd get the eventual name. In Ruby 2.0, _callee_ returns the name of the method from the caller's point of view, even if it has changed through aliasing or other means. So we can do this:

def example
  method(__callee__)
end

puts method(:example) === example # true :)

2 proxy clean room

Sometimes, We want trigger method missing event at class define level, To achieve this goal we can utilize a 'Blank Slates' object to act as a proxy:

class ProxyCleanRoom < BasicObject
  def initialize(callable)
    @listener = callable
  end

  def method_missing(msg, *args, &block)
    @listener.call msg, *args, &block
  end
end

class Example
  def self.trigger(&block)
    ProxyCleanRoom.new(method(:listener)).instance_eval &block
  end

  def self.listener(name, value)
    # At here, you can do a lot interesting things
    define_method(name) { value }
  end

  trigger {
    name 'Example' # It not conflict to the Example.name :)
    discription 'A Example for proxy clean room spell!'
  }
end

obj = Example.new

puts obj.name # Example
puts obj.discription # A Example for proxy clean room spell!

Use this spell, Class Example don't need to inherit the BasicObject but also obtain benefit from the proxy clean room.

第一版就在我手边,不知道第二版增加了什么内容?原书出来到中文版隔了两年,这次不会等太久吧。。。

这次应该会很快~好奇怪 ruby-china 的编辑器,对 markdown 的支持不是很正确呀。

我擦了。。。今天刚入手了第一版。。

@Ryan 就第一部分给我的感觉来看,其实新版只是修订了下,加入了 ruby2 的内容;我想变化最大的应该会是第二部分,关于 Rails 源码讲解的部分,这次是 4.0 的源码实例,而且第二部分以及附录都还没有动工,所以要成书出版应该还早,你还是先看第一版吧。

#7 楼 @scriptfans 好,先啃完第一本。

前两天还在翻保罗的 blog,没有一丝要出第二版的迹象呢。 这倒是个好消息,赶紧出吧。

@blackanger 还早呢,目前只完成了一半,我估计得明年第二季度了。

@lgn21st 今天出版社答复了,不能外传,囧……

等明年出来看哇~~!

@scriptfans 楼主在翻译书么?

@scriptfans 英文版已经看完 在致谢里还看到了楼主琳琳的小狗 这版比起第一版有很多很棒的新内容 就是英文书略贵 还只能看电子版 还是希望能有本纸质的翻起来有感觉啊 中文版拜托了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号