1

I have a Ruby template engine that uses eval(code_string, binding, source_file) and every time this is called Ruby must parse the code string and it is perhaps not as fast as it could be.

I'd like to find an alternative, such as converting code_string into a method, e.g. method = eval("lambda { #{code_string} }"), before executing it, theoretically eval(method, binding, source_file).

But the problem is, I need to provide a binding and a source file name (and possibly a line number). Basically, I want eval to be able to take a lambda as a first argument, but so far I can't find a way to do this.

ioquatix
  • 1,411
  • 17
  • 32
  • Sounds like a [XY problem](http://meta.stackexchange.com/a/66378) to me. – spickermann Dec 10 '14 at 01:46
  • The problem is to make `eval` more efficient by parsing and converting the code to something executable. `eval` is called multiple times with the same inputs. – ioquatix Dec 10 '14 at 02:02

1 Answers1

0

After doing some more digging, I found this was a relatively common problem after all:

For a "solution" to this exact problem: Changing the binding of a Proc in Ruby

For some more discussion from the Ruby forum: https://www.ruby-forum.com/topic/184259

In the end, it appears that what I want is not possible to do efficiently (i.e. without eval at some point). But, I can replace my code to use instance_eval though it isn't as flexible, but more efficient.

Community
  • 1
  • 1
ioquatix
  • 1,411
  • 17
  • 32