以前笹田さんに、procのソースが見たいとお願いしてみたことがあったのですが、
それをRubyコードレベルで実現するライブラリを見つけたので紹介します。
proc_sourc e.rb
I wrote this a while ago and it works by extracting a proc's origin file
name and line number from its .inspect string and using the source code
(which usually does not have to be read from disc) -- it works with
procs generated in IRB, eval() calls and regular files. It does not work
from ruby -e and stuff like "foo".instance_ eval "lambda {}".source
probably doesn't work either.
オリジナルはメールの添付ファイルとしてくっついているので、ソースを見たい場合は
こちらから見るといいかもしれません。
使い方はこんな感じです、
1 code = proc{puts "Hello World"} 2 puts code.source #=> puts "Hello World"
これは久々に面白いものを見た気がします。
posted by
genki
on Thu 3 Jul 2008
at 13:43