From: Quintus Date: 2012-09-30T06:45:08+09:00 Subject: Re: general newbie qustion --Sig_/J13tooiHqni8s8Sq4GxA311 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Am Sun, 30 Sep 2012 05:44:58 +0900 schrieb George Spak : > Justin T. wrote in post #1078043: > > Put simply: > > > > "print x" will print just the letter x. > > "print #{x}" parses out whatever the variable x is. >=20 > Ah that makes sense. Much appreciate the help. Unfortunately, the > docs are of little halp. This must be buried deep in them. It=E2=80=99s not correct.=20 x =3D 6 print x This will print out the number 6, which is the current value of x. To get the letter x, you want print "x" . That being said, #{} simply is a so-called string interpolation. These two statements are basically equal: str =3D "x is " + x.to_s + "!" str =3D "x is #{x}!" There are some subtle differences between them, namely regarding the number of string objects created. It is generally considered good coding practive using string interpolation if you don=E2=80=99t have a reas= on to do otherwise. Note that #{} can perfectly contain any valid Ruby expression; if you want to push it to its limits, you can even define a class inside it. Whatever the expression returns, will be converted to a string using the #to_s method (which in the case of strings happens to return the string itself). In contrast to this, the plus expression wants to use its argument as-is and won=E2=80=99t try to call #to_s on it. Compare: x =3D 6 str =3D "x is #{x}" str =3D "x is " + x While the former will work as expected (calling Fixnum#to_s, which returns the string "6"), the latter crashes with a TypeError, because you tried to combine a number with a string. Chaining is not the same as interpolation. Vale, Marvin --=20 Blog: http://pegasus-alpha.eu/blog ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign () - Stoppt HTML-E-Mail /\ | - Against HTML E-Mail /\ - Stoppt propriet=C3=A4re Anh=C3=A4nge | - Against proprietary attachm= ents www.asciiribbon.org/index-de.html | www.asciiribbon.org --Sig_/J13tooiHqni8s8Sq4GxA311 Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iQEcBAEBAgAGBQJQZ2vDAAoJELh1XLHFkqhavpQIAIZNUBX3jQTP17aY34Z8vH1n bleK+Zcx2RmUhOCBsM3yWki5p5iNKta/lc/5d1Yflh5zCxAXASGb+MO1A3v92z+9 MFCDYnvyAGqrytNmIYn50SggWT3MZcqK8n16D1HBDNbA8ZrmZmypxjwy4V8h+Adl sSlQIj+T9dn4oKyTvf7sGF+H8n6pdKPkv7tZgfm//Vwv/qkDLsE61fKcL2T5lwzO kcWSY/tQxJwO/8oUrqSbB53Wnc2yTE5MkPVTeaBfZqrQT0gIWd563MhoRpOUZwpb rbHQ6ldssTbpmxv8kcPcfuI9Qn2y2n2bHs+bKEpHhOeBtv+ws+KPqHq/R+3/DG4= =5gXn -----END PGP SIGNATURE----- --Sig_/J13tooiHqni8s8Sq4GxA311--