Using something like 'load' on IO Stream ?

  • Thread starter Philippe Philos
  • Start date
P

Philippe Philos

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
 
J

Jeremy Bopp

Tony Arcieri wrote in post #966933:

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
 
W

Walton Hoops

Tony Arcieri wrote in post #966933:
I would prefer another solution.
Lucas.
Reasoning? AFAIK there isn't another solution. This is pretty much
what eval was made for.
 
J

John Morrice

Tony Arcieri wrote
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
 
B

Brian Candler

Philippe Philos wrote in post #967004:
Tony Arcieri wrote in post #966933:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top