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

···

--
Posted via http://www.ruby-forum.com/.

eval

···

On Tue, Dec 7, 2010 at 10:37 AM, Philippe Philos <plucas@technologies.eu>wrote:

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

--
Posted via http://www.ruby-forum.com/\.

--
Tony Arcieri
Medioh! Kudelski

Tony Arcieri wrote in post #966933:

eval

I would prefer another solution.
Lucas.

···

--
Posted via http://www.ruby-forum.com/\.

why? that's exactly what eval is for...

···

On Dec 7, 2010, at 2:51 PM, Philippe Philos wrote:

Tony Arcieri wrote in post #966933:

eval

I would prefer another solution.
Lucas.

--
Posted via http://www.ruby-forum.com/\.

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

···

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

Tony Arcieri wrote in post #966933:

eval

I would prefer another solution.

Reasoning? AFAIK there isn't another solution. This is pretty much what eval was made for.

···

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

Tony Arcieri wrote in post #966933:

eval

I would prefer another solution.
Lucas.

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

Please state your requirements.

Kind regards

robert

···

On Tue, Dec 7, 2010 at 9:51 PM, Philippe Philos <plucas@technologies.eu> wrote:

Tony Arcieri wrote in post #966933:

eval

I would prefer another solution.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Philippe Philos wrote in post #967004:

Tony Arcieri 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.

···

--
Posted via http://www.ruby-forum.com/\.