From: Jacob Fugal Date: 2006-05-27T05:02:40+09:00 Subject: Re: Preserving Leading Zeros Using sprintf On 5/26/06, Jeremy Smith wrote: > a=3 > b=8 > [a,b].collect! { |c| c = sprintf('%02d', c) } > puts a # prints out 3 and not 03 You've been bit by the "variables == objects" mentality. When you perform your #collect!, each *element* of the receiver of #collect! is replaced by the result of the yield. See this: a = 3 b = 8 c = [a, b] c.collect! { |x| x = sprintf('%02d', x) } puts c[0] # prints '03' So when #collect! operates on the first element, it's essentially as such: c[0] = sprintf('%02d', c[0]) This puts a *new* object in that slot of the array; it *doesn't* alter the object that was previously in that slot. The variable a still refers to the old object, not the new one. Jacob Fugal