Ruby中的自修改代码 [英] Self-modifying code in Ruby

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

问题描述

我担心在Ruby中编写自我修改代码。并且通过自我修改,我的意思是能够编写将代码块作为输入值的函数,并且基于此输出另一个代码块。 (我不是在问运行时重新定义方法的基础)。



我可能想做的是,例如,有以下块,

  _x_ = lambda {| a,b,c,d | b + c} 

可以注意到参数 a d 根本不使用在body中,所以我想要一个函数eg。 #strip 可移除它们,

  x = _x_.strip 

这应该产生与写作相同的结果:

  x = lambda {| b,c |现在在Lisp中,这将是很容易的,因为Lisp代码是易于操作的数据。但我不知道如何操纵Ruby代码。我可以解析它例如。由

  RubyVM :: InstructionSequence.disassemble(x)

但是,基于这个,我怎么写一个修改的块呢?我想做的其他例子是例如

  y = lambda {A + B} 
.deconstantize
#应该给块与说
lambda {| _A,_B | _A + _B}



到目前为止,在Ruby中,我从来没有遇到过我不得不承认有些事情是不可能的。但是这一次,直觉的感觉告诉我,我可能已经遇到了美妙结构的代码与具有少量语法的代码的根本弱点(这将是Lisp)。请告诉我。

解决方案

Boris你一定要依赖Ruby从这里开始吗?



为什么不只是创建自己的情境特定的语言,化学家可以使用只是为了以最方便的方式表达他们的公式。然后为这个化学表达式语言创建一个简单的解析器和编译器。



我的意思是这个解析器和编译器将解析和编译化学家在他们的Ruby代码中写的表达式。然后你可以:

  ChemicalReaction.new(...,[ATP] * [GDP] * NDPK_constant)

Voila:极具弹性。



如果可用性是您的主要关注,我会采取的方法。已经写出lambda似乎是一个不必要的麻烦的事情,我在这里,如果你想做的是用最紧凑的方式表达一些域特定的公式。


I am concerned about writing self-modifying code in Ruby. And by self-modifying, I mean being able to write functions that take a code block as an input value, and output another code block based on this. (I am not asking about basics such as redefining methods at runtime.)

What I might want to do is, for example, having the following block,

_x_ = lambda { |a, b, c, d| b + c }

one can notice that arguments a and d are not used in the body at all, so I would like a function eg. #strip to remove them,

x = _x_.strip

which should produce same result as writing:

x = lambda { |b, c| b + c }

Now in Lisp, this would be easy, since Lisp code is easily manipulable data. But I do not know how to manipulate Ruby code. I can parse it eg. by

RubyVM::InstructionSequence.disassemble( x )

But how, based on this, do I write a modified block? Other examples of what I would want to do are are eg.

y = lambda { A + B }
y.deconstantize
# should give block same as saying
lambda { |_A, _B| _A + _B }

So far, in Ruby, I have never encountered a situation where I had to concede that something is not possible. But this time, gut feeling tells me that I might have encountered the fundamental weakness of beautifully structured code vs. code with little syntax to speak about (which would be Lisp). Please enlighten me.

解决方案

Boris do you necessarily have to rely on Ruby to begin with here?

Why not just create your own situation-specific language that the chemists can use just for the purpose to express their formulas in the most convenient way. Then you create a simple parser and compiler for this "chemical expression language".

What I mean is this parser and compiler will parse and compile the expressions the chemists write in their Ruby code. Then you could have:

ChemicalReaction.new(..., "[ATP] * [GDP] * NDPK_constant")

Voila: ultimate flexibility.

That's the approach I would take if usability is your main concern. Already writing out "lambda" seems like an unnecessarily cumbersome thing to me here, if all you want to do is express some domain-specific formula in the most compact way possible.

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

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