Levin Alexander <levin / grundeis.net> writes: > Hi, > > I'd like to propose Fixnum#to_a: > > class Fixnum > def to_a(base=10,min_length=0) > self.to_s(base).rjust(min_length).split(//).map { |c| c.to_i(base) } > end > end > > 12345.to_a #=> [1,2,3,4,5] > 42.to_a(2) #=> [1,0,1,0,1,0] > 23.to_a(4,5) #=> [0,0,1,1,3] > -10.to_a #=> ??? > > This relies on to_s and therefore does not work with a base > 36. I > don't know how this should word for negative numbers. It should be twos-complement for negative numbers, of course. ;-) > Maybe the default base should be 2 to be consistent with Fixnum#[] > > Thoughts? IMO a useful method, *but*: Don't call it #to_a. to_a has certain duck-typing aspects, and this usage is too rare to be triggered a lot (just think of a = 42; b = [*a]). Also, you may want to extend it to be like APL's "encode" (tack), so you can do stuff like: 1776.encode(8) # => [1, 0, 2, 2] (octal) 105246.encode(0, 1760, 3, 12) # => [1, 1163, 1, 6] # [miles, yards, feet, inches] Then, we'd also need an "decode": [14, 12, 20, 51].decode(0, 24, 60, 60) # => 1254057 # [days, hours, minutes, seconds] (The examples were taken from "APL\360 Primer, Student Text, IBM, 1969".) > Viele Grüße, > Levin -- Christian Neukirchen <chneukirchen / gmail.com> http://chneukirchen.org