From: Chad Perrin Date: 2011-05-25T07:05:00+09:00 Subject: Re: "Local variable within code blocks do not interfere with those outside the block" --hOcCNbCCxyk/YU74 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, May 24, 2011 at 06:45:10PM +0900, Kaye Ng wrote: > I read this in a book. >=20 > " In Ruby 1.9, however, local variables used within code blocks will not > interfere with local variables located outside of the block." >=20 > I don't know if my code is wrong, but it looks to me like the local > variable inside the code block DOES interfere with the local variable > (with the same name) outside the code block. >=20 > x =3D [1, 2, 3, 4, 5] > var =3D 1 >=20 > x.each do > |number| (var =3D 10) > end >=20 > puts var Others are handling your question well enough, but I have a comment. It is more idiomatically Rubyish to format your block like this: x.each do |number| var =3D 10 end =2E . . rather than like this: x.each do |number| (var =3D 10) end I would find the latter style (your version) much more difficult to read for complex code samples, and I suspect I am not alone in that. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --hOcCNbCCxyk/YU74 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk3cKDYACgkQ9mn/Pj01uKVUjACfUYPIZos0AgIcrOdbrOZXBjBP 95YAoNj+lN/fI4HJRNuCYL5JPUgskB3h =s/oo -----END PGP SIGNATURE----- --hOcCNbCCxyk/YU74--