[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: How to take output of eval into a string?

Morton Goldberg

9/6/2006 2:23:00 PM

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.bl...

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



4 Answers

Morton Goldberg

9/6/2006 3:24:00 PM

0

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.bl...
>
> 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

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
The answer is 2
The answer is 2
</results>

Maybe that will help you.

Regards, Morton



James Gray

9/6/2006 6:18:00 PM

0

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


Jano Svitok

9/6/2006 6:52:00 PM

0

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.

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)

Logan Capaldo

9/8/2006 4:11:00 AM

0


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

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