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 '-'
Written by Emad Elsaid
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#