作者godfat (godfat 真常)
看板Ruby
标题Re: [Quiz] Hello, world? (#158)
时间Mon Jun 22 15:04:56 2009
倒楣,断线 p 币少一堆 @@
※ 引述《godfat (godfat 真常)》之铭言:
: #!/usr/bin/env ruby
: # encoding: utf-8
: %w[ Class <<self Symbol ].each{ |klass|
: eval <<-RUBY.split(/真常/).join
: class #{klass}
: 真常 define_method(:method_missing, &:say)
: end
: RUBY
: }
: Class.send(:define_method, :const_missing,
: &lambda{ |c| cry c.to_s[0..-2] })
: def say s = nil
: print s, ' ' if s
: self
: end
: public :say
: sing Hello.World!
从这边开始,呼叫 sing, 但这个 method 不存在,於是会呼叫
上面定义的:
class <<self
def method_missing *args, &block
:say.to_proc(*args, &block)
end
end
也就是 top level 的 singlenton method.
Symbol#to_proc 约略可看成:
class Symbol
def to_proc
msg = self # this would make rubinius and jruby work
lambda{ |*args| args.shift.send(msg, *args) }
end
end
也就是 :to_s.to_proc[16, 16] # 16.to_s(16) # => 10
因此 sing Hello.World! 可以看成:
:say.to_proc[:sing, Hello.World!]
再把 to_proc 拆开,就是:
:sing.send(:say, Hello.World!)
而事实上这边其实要先看 Hello.World! 是什麽,
因为 ruby 是 strict 的语言,argument 要先算出来才能执行 function.
要先算出 Hello 是什麽,然後再对他叫 World!,
最後的结果才能丢给 :sing.
复习:
sing Hello.World! # => :sing.send(:say, Hello.World!)
接下来就看 Hello 是什麽了。由於没有这个东西,
所以会呼叫 const_missing. 而上面定义了 Class
#const_missing,
因此 top level 的 const_missing 也会有效,
因为 Object 本身也是继承自 Class 的:
(class << Object; self; end).ancestors
# => [Class, Module, Object, Kernel, BasicObject]
这边实际上就会变成呼叫:
lambda{ |c| cry c.to_s[0..-2] }
Hello 就会变成:
cry 'Hell'
这边 cry 同样是 method_missing, receiver 则是 Object,
因为 top level 的 const lookup 是透过 Object (global)
这边就是用 Class
#method_missing, 因此跟上面一样改写成:
:say.to_proc[:cry, 'Hell'] # => :cry.send(:say, 'Hell')
而 say 由於定义在 top level, 也就是 Kernel#say,
虽然是 private 的,但 send 本身无视 method visibility.
这边又会变成:
print 'Hell', ' ' if 'Hell'
就会印出 Hell 出来。回传 self 则是 :cry
回到前面,原本的:
:sing.send(:say, Hello.World!)
就会变成:
:sing.send(:say, :cry.World!)
则会呼叫 Symbol
#method_missing, 变成:
:sing.send(:say, :World!.send(:say))
因此 say 就变成没有 argument:
print nil, ' ' if nil
:World!
say 里第一行就没效果,第二行回传 :World!, 也就是 self
最後整个式子就会是:
:sing.send(:say, :World!)
回到 top level 的 say, 变成:
print :World!, ' ' if :World!
:sing
整个结果就会是 Hell World!
: puts
: __END__
: Apache License 2.0
希望没讲错的地方,很复杂看半天才写出来的 @@
另外这边 public: say 没有效果,还有修正 Symbol#to_proc,
这样在 rubinius 和 jruby 底下就都能跑了
不确定这算他们的 bug 还是 feature
--
#!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》
# if a
dog nailed
extra legs that
http://webptt.com/cn.aspx?n=bbs/Ruby/index.html
#
walks like an octopus, and Welcome ~
Ruby@ptt~
#
talks like an octopus, then
◢█◣ http://www.ruby-lang.org/
# we are happy to treat it as
█ http://www.ruby-doc.org/
# if it were
an octopus.
◥ ◤ http://www.rubyforge.org/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.128.121.85