From: "Darryl L. Pierce" Date: 2011-08-10T04:05:21+09:00 Subject: Re: *WHY* does this not work? --im83/wVv0jiGQj4J Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Aug 10, 2011 at 03:52:59AM +0900, serialhex wrote: > ok, so code: >=20 > str =3D 'foo' > =3D> "foo" > str.each_char do |c| > c =3D 'x' > end > =3D> "foo" >=20 > 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. > i would expect it to be 'xxx'! this makes no > sense, and is *QUITE* annoying... If you look at the source code for each_char you'll see that its return value is the original string itself. Which is why you see "foo" returned. Sometimes it's your expectation that's wrong and not the library you're using. ;) --=20 Darryl L. Pierce http://mcpierce.multiply.com/ "What do you care what people think, Mr. Feynman?" --im83/wVv0jiGQj4J Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk5BhOoACgkQtddqM0QFuLIsYwCg0nX7ir7xZ8uChItfX8jBwMnZ dq4AoJ0Xn0MK7unOkijxsaOWegEdLImo =K/0F -----END PGP SIGNATURE----- --im83/wVv0jiGQj4J--