Over a million developers have joined DZone.

converting integer to an Array in #ruby

·

 

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 '-'
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}