Back

如何打印ruby的动态代码

发布时间: 2015-06-28 00:56:00

refer to:  http://stackoverflow.com/questions/1675053/printing-the-source-code-of-a-ruby-block

ruby最强的地方(之一)在于 meta programming. 那么了解到动态的代码的具体内容就很重要

默认ruby不提供  proc.inspect 这样的方法.还好 社区的战友们提供了这个工具. 

require 'sourcify'

hi_proc = proc { puts 'hihihi' }
#hi_proc = Proc.new { puts "hihihi" }  # 不能打印 Proc.new 的,只能打印  proc {} 出来的代码块.
puts hi_proc.to_source
# => proc { puts("hihihi") }

Back