How to take output of eval into a string?

#code
a = "puts 'I like Ruby'"
b = eval(a)
puts b

after running above code, it gives output

I like Ruby
nil

the above code prints b a nil, I want output of eval into a string!

How to do that? Please help!

Regards,
Anil Wadghule

···

--
Don't live to geek; geek to live.
http://anildigital.blogspot.com

Your code is equivalent to ...

b = puts 'I like Ruby'
puts b

... and produces the same results, which is nil because puts always returns nil. Eval returns whatever object comes out of its evaluation, which can be any kind of an object. You can't count on it returning a string.

Exactly what are you trying to capture into a string?

Regards, Morton

···

On Sep 6, 2006, at 9:02 AM, Anil Wadghule wrote:

#code
a = "puts 'I like Ruby'"
b = eval(a)
puts b

after running above code, it gives output

I like Ruby
nil

the above code prints b a nil, I want output of eval into a string!

How to do that? Please help!

Regards,
Anil Wadghule
-- Don't live to geek; geek to live.
http://anildigital.blogspot.com

P.S. Perhaps I should add an example of my own.

<code>
a = "1 + 1"
b = "The answer is " + eval(a).to_s
c = "The answer is #{eval(a)}"
d = "The answer is %d" % eval(a)
puts a, b, c, d
</code>

<results>
1 + 1
The answer is 2
</results>

Maybe that will help you.

Regards, Morton

···

On Sep 6, 2006, at 10:23 AM, Morton Goldberg wrote:

On Sep 6, 2006, at 9:02 AM, Anil Wadghule wrote:

#code
a = "puts 'I like Ruby'"
b = eval(a)
puts b

after running above code, it gives output

I like Ruby
nil

the above code prints b a nil, I want output of eval into a string!

How to do that? Please help!

Regards,
Anil Wadghule
-- Don't live to geek; geek to live.
http://anildigital.blogspot.com

Your code is equivalent to ...

b = puts 'I like Ruby'
puts b

... and produces the same results, which is nil because puts always returns nil. Eval returns whatever object comes out of its evaluation, which can be any kind of an object. You can't count on it returning a string.

Exactly what are you trying to capture into a string?

Regards, Morton

Morton Goldberg <m_goldberg <at> ameritech.net> writes:

> #code
> a = "puts 'I like Ruby'"
> b = eval(a)
> puts b

Exactly what are you trying to capture into a string?

I would guess that the intention is to capture STDOUT and get b == 'I like Ruby'

···

On Sep 6, 2006, at 9:02 AM, Anil Wadghule wrote:

Morton Goldberg <m_goldberg <at> ameritech.net> writes:

#code
a = "puts 'I like Ruby'"
b = eval(a)
puts b

Exactly what are you trying to capture into a string?

I would guess that the intention is to capture STDOUT and get b == 'I like Ruby'

>> require "stringio"
=> true
>> a = "puts 'I like Ruby'"
=> "puts 'I like Ruby'"
>> b = eval("begin $stdout = StringIO.new; #{a}; $stdout.string; ensure $stdout = STDOUT end")
=> "I like Ruby\n"

Hope that helps.

James Edward Gray II

···

On Sep 6, 2006, at 12:52 PM, Gareth Adams wrote:

On Sep 6, 2006, at 9:02 AM, Anil Wadghule wrote:

It helped me at least. I was playing with ERuby (the C version), I
wanted mainly to compare its speed to ERB, and this helped me to try
this without recompilation. (FYI: for a simple template, eruby seems
to take ~1/3 of the ERB time)

···

On 9/6/06, James Edward Gray II <james@grayproductions.net> wrote:

>> b = eval("begin $stdout = StringIO.new; #{a}; $stdout.string;
ensure $stdout = STDOUT end")

Hope that helps.

As an alternative (not exactly an equivalent one:)

% cat piper.rb
a = 'puts "I like ruby"'
s = ''
IO.popen("ruby", "w+") do |f|
   f.write a
   f.close_write
   s = f.read
end

puts s

% ruby piper.rb
I like ruby

···

On Sep 6, 2006, at 2:17 PM, James Edward Gray II wrote:

On Sep 6, 2006, at 12:52 PM, Gareth Adams wrote:

Morton Goldberg <m_goldberg <at> ameritech.net> writes:

On Sep 6, 2006, at 9:02 AM, Anil Wadghule wrote:

#code
a = "puts 'I like Ruby'"
b = eval(a)
puts b

Exactly what are you trying to capture into a string?

I would guess that the intention is to capture STDOUT and get b == 'I like Ruby'

>> require "stringio"
=> true
>> a = "puts 'I like Ruby'"
=> "puts 'I like Ruby'"
>> b = eval("begin $stdout = StringIO.new; #{a}; $stdout.string; ensure $stdout = STDOUT end")
=> "I like Ruby\n"

Hope that helps.

James Edward Gray II