From: Douglas Seifert Date: 2012-09-30T06:16:28+09:00 Subject: Re: general newbie qustion --f46d040711c558b89504caddaf24 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Sep 29, 2012 at 1:41 PM, Justin T. wrote: > Put simply: > > "print x" will print just the letter x. > "print #{x}" parses out whatever the variable x is. > > The above is incorrect. "print x" will convert whatever object x refers to a string and print it to the console. If x is already referring to a string, no conversion is done. 'print "#{x}"' will create a new string and interpolate into it the object to which x refers converted into a string. This always creates a new string, even if x is already referring to a string. If you see 'print "#{x}"', it might be a code smell, especially if x is referring to a string, because might be creating a useless copy of an existing string just to print it out. Interpolation is better used when you really do need to create a new string, for example: print "My variable is '#{x}'" There are various complex rules on how objects get converted to strings which you should read up on. -Doug --f46d040711c558b89504caddaf24 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Sat, Sep 29, 2012 at 1:41 PM, Justin = T. <lists@ruby-forum.com> wrote:
Put simply:

"print x" will print just the letter x.
"print #{x}" parses out whatever the variable x is.

<= br>
The above is incorrect. =A0

"pr= int x" will convert whatever object x refers to a string and print it = to the console. =A0If x is already referring to a string, no conversion is = done.
'print "#{x}"' will create a new string and interpol= ate into it the object to which x refers converted into a string. =A0This a= lways creates a new string, even if x is already referring to a string.=A0<= /div>

If you see 'print "#{x}"', it might b= e a code smell, especially if x is referring to a string, because might be = creating a useless copy of an existing string just to print it out.

Interpolation is better used when you really do need to= create a new string, for example:

print "My = variable is '#{x}'"

There are various= complex rules on how objects get converted to strings which you should rea= d up on.

-Doug
--f46d040711c558b89504caddaf24--