如何从 Proc 对象中提取代码? [英] How to extract the code from a Proc object?

查看:42
本文介绍了如何从 Proc 对象中提取代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 Proc 对象,是否可以查看其中的代码?

例如:

p = Proc.new{test = 0}

我需要以某种方式从已经创建的 Proc 对象中获取字符串test = 0".

解决方案

您可以使用 ruby2ruby图书馆:

<代码>>># 用 1.8.7 测试>>需要parse_tree"=>真的>>需要ruby2ruby"=>真的>>需要parse_tree_extensions"=>真的>>p = Proc.new{test = 0}>>p.to_ruby=>过程{测试= 0}"

您也可以将 proc 的这个字符串表示转回 ruby​​ 并调用它:

<代码>>>评估(p.to_ruby).调用0

<小时>

在此视频中详细了解 ruby​​2ruby:使用 ruby​​2ruby 进行黑客攻击.>

Given a Proc object, is it possible to look at the code inside it?

For example:

p = Proc.new{test = 0}

What I need is for some way to get the string "test = 0" from a Proc object that has already been created.

解决方案

You can use the ruby2ruby library:

>> # tested with 1.8.7
>> require "parse_tree"
=> true
>> require "ruby2ruby"
=> true
>> require "parse_tree_extensions"
=> true
>> p = Proc.new{test = 0}
>> p.to_ruby
=> "proc { test = 0 }"

You can also turn this string representation of the proc back to ruby and call it:

>> eval(p.to_ruby).call
0


More about ruby2ruby in this video: Hacking with ruby2ruby.

这篇关于如何从 Proc 对象中提取代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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