From: Sebastian Hungerecker Date: 2008-09-26T18:21:27+09:00 Subject: Re: How does Array's each method change its elements ? w wg wrote: > I want to know how does Array's each method (block) change its elements ? It doesn't. You do. > b = a.each { |x| x + 1} This executes "x + 1" for every element in the array. Compare the following: x = 5 x + 1 p x x is still 5. The expression x+1 does not change the value of x. > b = a.each { |x| x << "-" } x = "foo" x << "-" p x Prints "foo-" because << does modify the receiver. I.e. after calling << "-" the string will contain a "-", so that's what's happening to each element in the array. Another note: each returns the receiver, so if you do b = a.each {...} b and a will point to the exact same array no matter what happens in the block (unless you use break inside the block). HTH, Sebastian -- Jabber: sepp2k@jabber.org ICQ: 205544826