Turning long fixed number to array Ruby
问题 Is there a method in ruby to turn fixnum like 74239 into an array like [7,4,2,3,9] ? 回答1: You don't need to take a round trip through string-land for this sort of thing: def digits(n) Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 } end ary = digits(74239) # [7, 4, 2, 3, 9] This does assume that n is positive of course, slipping an n = n.abs into the mix can take care of that if needed. If you need to cover non-positive values, then: def digits(n) return [0] if(n == 0) if(n < 0) neg