From: Chad Perrin Date: 2011-05-27T02:38:45+09:00 Subject: Re: "Local variable within code blocks do not interfere with those outside the block" --huq684BweRXVnRxX Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 26, 2011 at 06:20:14PM +0900, Bala TS wrote: > Here x is a variable(array_variable) it contains 5 elements >=20 > x =3D [1,2,3,4,5] >=20 >=20 > x.each do |r|{puts "#{r}"} > end This code is not correct -- and furthermore, the interpoation syntax you are using is unnecessary. Try this: x.each {|r| puts r } The block variable should be identified *inside* the opening of the block, and when using braces you do not need "do" and "end". Alternatively, if you want to use "do" and "end", you do not need braces: x.each do |r| puts r end The only reason to use the interpolation syntax here is if you want to output more than just the contents of the block variable: x.each {|r| puts "At #{Time.now}, r contains #{r}."; sleep 3 } At Thu May 26 11:37:25 -0600 2011, r contains 1. At Thu May 26 11:37:28 -0600 2011, r contains 2. At Thu May 26 11:37:31 -0600 2011, r contains 3. At Thu May 26 11:37:34 -0600 2011, r contains 4. At Thu May 26 11:37:37 -0600 2011, r contains 5. =3D> [1, 2, 3, 4, 5] --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --huq684BweRXVnRxX Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk3ejMcACgkQ9mn/Pj01uKW1vACg0ECnwWrp/XNby9o3cahsIukn ZsoAn3peuIECgTpkL8D+JcSJLq9X2R0i =s4g9 -----END PGP SIGNATURE----- --huq684BweRXVnRxX--