Ruby 板


LINE

倒楣,斷線 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/m.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







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP