打印 Ruby 块的源代码 [英] Printing the source code of a Ruby block

查看:56
本文介绍了打印 Ruby 块的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受块的方法.

I have a method that takes a block.

显然我不知道要传入什么,出于奇怪的原因,我不会进入这里,我想打印块的内容.

Obviously I don't know what is going to be passed in and for bizarre reasons that I won't go into here I want to print the contents of the block.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以使用实现 to_ruby 方法的 Ruby2Ruby 来做到这一点.

You can do this with Ruby2Ruby which implements a to_ruby method.

require 'rubygems'
require 'parse_tree'
require 'parse_tree_extensions'
require 'ruby2ruby'

def meth &block
  puts block.to_ruby
end

meth { some code }

将输出:

"proc { some(code) }"

我也会看看 Github 的 Chris Wanstrath 的精彩演讲 http://goruco2008.confreaks.com/03_wanstrath.html 他展示了一些有趣的 ruby​​2ruby 和解析树用法示例.

I would also check out this awesome talk by Chris Wanstrath of Github http://goruco2008.confreaks.com/03_wanstrath.html He shows some interesting ruby2ruby and parsetree usage examples.

这篇关于打印 Ruby 块的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆