Logan Capaldo
9/8/2006 4:11:00 AM
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