From: "Darryl L. Pierce" Date: 2011-08-10T04:20:27+09:00 Subject: Re: *WHY* does this not work? --D5tFrmRBv7YOLFOK Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Aug 10, 2011 at 04:11:05AM +0900, serialhex wrote: > On Tue, Aug 9, 2011 at 3:05 PM, Darryl L. Pierce wro= te: >=20 > > On Wed, Aug 10, 2011 at 03:52:59AM +0900, serialhex wrote: > > > ok, so code: > > > > > > str =3D 'foo' > > > =3D> "foo" > > > str.each_char do |c| > > > c =3D 'x' > > > end > > > =3D> "foo" > > > > > > WHY does str =3D=3D 'foo' still?? > > > > Because each_char is not destructive to the original string's value. It > > moves through the string as an array of characters and then passes each > > one _by value_ to the block supplied. So while you might be able to > > locally alter the value for that character, you don't have access to the > > original string itself in the block. >=20 > so is there a way to do that? easily without jumping through hoops of > fire?? You want to iterate through a string and change individual characters? Why not just do: str =3D "farkle" (0...str.size).each {|index| str[index] =3D 'x'} --=20 Darryl L. Pierce http://mcpierce.multiply.com/ "What do you care what people think, Mr. Feynman?" --D5tFrmRBv7YOLFOK Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk5BiBoACgkQtddqM0QFuLJJswCfVyq8+T37Up52G7pFmHqYINUg uO0An3ZAamy36VRVI3EJI3k8cW/QsP+3 =mV71 -----END PGP SIGNATURE----- --D5tFrmRBv7YOLFOK--