Forum: Ruby Preserving Leading Zeros Using sprintf

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dd79a599b9fb231c142378a816ff8892?d=identicon&s=25 Jeremy Smith (Guest)
on 2006-05-26 21:54
From http://www.rubycentral.com/ref/ref_c_array.html#collect_oh:

"Invokes block once for each element of arr, replacing the element with
the value returned by block."

Okay, so I attempt to execute the following code:


a=3
b=8
[a,b].collect! { |c| c = sprintf('%02d', c) }
puts a # prints out 3 and not 03


However, this code:


a=3
b=8
(a,b) = [a,b].collect! { |c| c = sprintf('%02d', c) }
puts a # prints out 03


Why is that?

If it's of any use, this is ruby 1.8.4 (2005-12-24) [i386-cygwin].

Thanks for demystification.
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-05-26 22:04
(Received via mailing list)
On 5/26/06, Jeremy Smith <jeremy.smith@gmail.com> 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
This topic is locked and can not be replied to.