Last Updated: February 25, 2016
·
3.523K
· blazeeboy

Converting Integer to Array with ruby

i faced this problem a lot while solving projecteuler.net problems, so to convert integer to an array of digits

class Integer
    def to_a
        arr = []
        tmp = self
        while tmp>0
            arr << tmp%10
            tmp /= 10
        end
        arr.reverse
    end
end

# usage
print 32455.to_a
print 433456.to_a
print 345434.to_a.sort
print 344543.to_a.join '-'