Using something like 'load' on IO Stream?

Hello,

Is there a way to load a Ruby script in text, not a .so or .dll, from an
IO Stream or a StringIO ?

Thanks.

Lucas

eval

On 12/7/2010 2:51 PM, Philippe P. wrote:

Tony A. wrote in post #966933:

eval

I would prefer another solution.

Perhaps you could elaborate a bit on what makes the use of eval
unsatisfactory? In the end, I think it’s what you’re stuck with given
your needs, but you might be able to wrap it up within another method in
order to hide what you dislike about it.

-Jeremy

why? that’s exactly what eval is for…

Tony A. wrote in post #966933:

eval

I would prefer another solution.
Lucas.

On 12/7/2010 1:51 PM, Philippe P. wrote:

Tony A. wrote in post #966933:

eval
I would prefer another solution.
Lucas.
Reasoning? AFAIK there isn’t another solution. This is pretty much
what eval was made for.

Tony A. wrote

eval

I would prefer another solution.
Lucas

eval is going to be the least computationally expensive, most portable,
most reliable, most obvious and easiest way to do this. But it isn’t
the only way!

Given:

cmd = ““puts ‘hello world’””

You could:

system “ruby -e #{cmd}”

or:

system “echo #{cmd} | ruby”

Or if you were to do this:

f = File.open “cmd.rb”, “w”
f.write “puts ‘hello world!’”
f.close

You could do:

system “ruby cmd.rb”

Or even better:

load “cmd.rb”

You might think I’m crazy but sometimes I store all my ruby programs in
text files!

Johnny

Philippe P. wrote in post #967004:

Tony A. wrote in post #966933:

eval

I would prefer another solution.

Have a look in the ruby C source for load() and eval(), and you’ll see
they’re basically the same. They call rb_compile_file and
rb_compile_string respectively, which in turn call yycompile.

So if foo is a StringIO, then

eval(foo.string)

is exactly what you asked for.

On Tue, Dec 7, 2010 at 9:51 PM, Philippe P. [email protected]
wrote:

Tony A. wrote in post #966933:

eval

I would prefer another solution.

Please state your requirements.

Kind regards

robert