以前笹田さんに、procのソースが見たいとお願いしてみたことがあったのですが、
それをRubyコードレベルで実現するライブラリを見つけたので紹介します。
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.
オリジナルはメールの添付ファイルとしてくっついているので、ソースを見たい場合は
こちらから見るといいかもしれません。
使い方はこんな感じです、
ruby>>
code = proc{puts "Hello World"}
puts code.source #=> puts "Hello World"
<<--
これは久々に面白いものを見た気がします。
posted by
genki on Thu 3 Jul 2008 at 13:33 with 0 comments