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

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