From: Brian Candler Date: 2012-08-16T06:04:10+09:00 Subject: Re: Eval without eval? Ryan Davis wrote in post #1072466: >> I am having issues with Sourcify/ ParseTree not evaluating eval-ed codes >> and thinking if that could help. > > If you have the string, ruby_parser is probably your best bet. > > If you're having problems with either of those two gems, you should file > a bug on one or both instead of getting unhelpful advice here (eg > Brian). I found the question to be unclear to the point of senseless. (1) If the question was intended to mean "how can I convert an arbitrary string containing ruby code to the value which would result from executing that code?" (and you don't care about the side effects of executing that code), then it seems to me you may as well just eval it. But the OP said "not using any form of eval", without giving any reason. (2) If the question was "how can I eval an arbitrary string of ruby code, but limited to a safe subset of functions" (e.g. disable calls to Kernel#system) then it becomes a sandboxing question. You could run a ruby-in-ruby implementation configured to execute certain constructs but not others; or you could run something like the sandbox gem for MRI. (3) The question actually was 'how "Proc.new{}" (String) could be converted into Proc.new{} (Code)', which is a highly limited form of (2). Even this is unclear when it says (Code), and I took this to mean (Object) or (Value). I was not being 100% facetious in answering this literally. If you have only a handful of different Ruby expressions you want to recognise and turn into real objects, then I think a case statement would be a perfectly reasonable way to proceed. case str when /\A(\d+)\z/ $1.to_i ... etc else raise "Invalid expression" end -- Posted via http://www.ruby-forum.com/.