#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 W.
#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 W.
On Sep 6, 2006, at 9:02 AM, Anil W. wrote:
the above code prints b a nil, I want output of eval into a string!
How to do that? Please help!
Regards,
Anil W.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 10:23 AM, Morton G. wrote:
nil
Your code is equivalent to …Regards, Morton
P.S. Perhaps I should add an example of my own.
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
1 + 1
The answer is 2
The answer is 2
The answer is 2
Maybe that will help you.
Regards, Morton
On Sep 6, 2006, at 12:52 PM, Gareth A. wrote:
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”
=> truea = “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 G. II
Morton G. <m_goldberg ameritech.net> writes:
On Sep 6, 2006, at 9:02 AM, Anil W. 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’
On 9/6/06, James Edward G. II [email protected] 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)
On Sep 6, 2006, at 2:17 PM, James Edward G. II wrote:
puts b
b = eval(“begin $stdout = StringIO.new; #{a}; $stdout.string;
ensure $stdout = STDOUT end”)
=> “I like Ruby\n”Hope that helps.
James Edward G. 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs