Re: [Quiz] Hello, world? (#158)

看板Ruby作者 (godfat 真常)時間16年前 (2009/06/22 15:04), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
倒楣,斷線 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://www.ptt.cc/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
文章代碼(AID): #1AFoqRpx (Ruby)
討論串 (同標題文章)
文章代碼(AID): #1AFoqRpx (Ruby)