Prev: upgrading gems with native dependencies
Next: Can't install Mongrel and My SQL gem on Windows XP 64 bit
From: Amir Ebrahimifard on 3 Aug 2010 04:19 Hi What does happen for "num" variable in this code : array = [1,2,3,4,5] x = 1 num = 1 array.each { |num| puts num*20 + x } ( after this code when I type "puts num , ruby return 5 ! why? ) -- Posted via http://www.ruby-forum.com/.
From: Eugen Ciur on 3 Aug 2010 04:41 |num| inside block iterates through array's items. Last item in array is 5, thus |num| value will be the one from last iteration of 'each' method. It is better to rename |num| to something else, |item| for example. ----- Eugen On 08/03/2010 11:19 AM, Amir Ebrahimifard wrote: > Hi > What does happen for "num" variable in this code : > > array = [1,2,3,4,5] > x = 1 > num = 1 > array.each { |num| puts num*20 + x } > > ( after this code when I type "puts num , ruby return 5 ! why? ) >
From: Guglielmo Fachini on 3 Aug 2010 04:53 Eugen Ciur wrote: > |num| inside block iterates through array's items. > Last item in array is 5, thus |num| value will be the one from last > iteration of 'each' method. > It is better to rename |num| to something else, |item| for example. > ----- > Eugen I've tried it in my machine and the output is 1... C:\Users\Guglielmo>ruby -v ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32] C:\Users\Guglielmo>ruby array = [1,2,3,4,5] x = 1 num = 1 array.each { |num| puts num*20 + x } puts num ^D 21 41 61 81 101 1 -- Posted via http://www.ruby-forum.com/.
From: Eugen Ciur on 3 Aug 2010 05:02 Look here http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS Since v 1.9.1 block arguments (in our case |num|) are always local, i.e in 'each' block |num| will not conflict with outer 'num' variable. In your machine you have 1.9.1 version. On 08/03/2010 11:53 AM, Guglielmo Fachini wrote: > Eugen Ciur wrote: > >> |num| inside block iterates through array's items. >> Last item in array is 5, thus |num| value will be the one from last >> iteration of 'each' method. >> It is better to rename |num| to something else, |item| for example. >> ----- >> Eugen >> > I've tried it in my machine and the output is 1... > > C:\Users\Guglielmo>ruby -v > ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32] > > C:\Users\Guglielmo>ruby > array = [1,2,3,4,5] > x = 1 > num = 1 > array.each { |num| puts num*20 + x } > puts num > ^D > 21 > 41 > 61 > 81 > 101 > 1 > ----- Eugen
From: Amir Ebrahimifard on 3 Aug 2010 05:45 Eugen Ciur wrote: > Look here > http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS > > Since v 1.9.1 block arguments (in our case |num|) are always local, > i.e in 'each' block |num| will not conflict > with outer 'num' variable. In your machine you have 1.9.1 version. It means in ruby v 1.8.6 block variable and local variable are the same , and not difference between them ? -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: upgrading gems with native dependencies Next: Can't install Mongrel and My SQL gem on Windows XP 64 bit |