Zero padding in Fixnum#to_s

What about adding zero padding (rjust by “0”) by specifying an optional
second argument? Don’t you think it’s little nicer to be able to say:

puts “64bit checksum is #{chk.to_s(16, 16)}”

instead of having to say:

puts “64bit checksum is #{chk.to_s(16).rjust(16, ‘0’)}”

I believe there’s a pretty frequent need for padding when using
Fixnum#to_s so perhaps this functionality can be added for convenience.
Besides, this change is backward compatible.

···


dave

Hi,

···

In message “Zero padding in Fixnum#to_s” on 04/03/19, David Garamond lists@zara.6.isreserved.com writes:

What about adding zero padding (rjust by “0”) by specifying an optional
second argument? Don’t you think it’s little nicer to be able to say:

puts “64bit checksum is #{chk.to_s(16, 16)}”

instead of having to say:

puts “64bit checksum is #{chk.to_s(16).rjust(16, ‘0’)}”

I prefer

printf “64bit checksum is %016x\n”, chk

						matz.

Yukihiro Matsumoto wrote:

What about adding zero padding (rjust by “0”) by specifying an optional
second argument? Don’t you think it’s little nicer to be able to say:

puts “64bit checksum is #{chk.to_s(16, 16)}”

instead of having to say:

puts “64bit checksum is #{chk.to_s(16).rjust(16, ‘0’)}”

I prefer

printf “64bit checksum is %016x\n”, chk

What about base other than 2, 8, 10, and 16 (yes, sorry, bad example. I
was using base 36 actually).

···


dave

Hi,

···

In message “Re: Zero padding in Fixnum#to_s” on 04/03/19, David Garamond lists@zara.6.isreserved.com writes:

Yukihiro Matsumoto wrote:

What about adding zero padding (rjust by “0”) by specifying an optional
second argument? Don’t you think it’s little nicer to be able to say:

puts “64bit checksum is #{chk.to_s(16, 16)}”

instead of having to say:

I prefer

printf “64bit checksum is %016x\n”, chk

What about base other than 2, 8, 10, and 16 (yes, sorry, bad example. I
was using base 36 actually).

In that case, use rjust(16, “0”), which is designed for the particular
case. I feel

puts “64bit checksum is #{chk.to_s(16).rjust(16, ‘0’)}”

is lot easier to understand than

puts “64bit checksum is #{chk.to_s(16, 16)}”

						matz.

Yukihiro Matsumoto wrote:

In that case, use rjust(16, “0”), which is designed for the particular
case. I feel

puts “64bit checksum is #{chk.to_s(16).rjust(16, ‘0’)}”

is lot easier to understand than

puts “64bit checksum is #{chk.to_s(16, 16)}”

Ok, you’re the boss :slight_smile: Thanks for considering the idea.

···


dave