Split an Integer into an Array
I needed this for implementing a arabic to roman numeral converter. I needed to split the integer into an array of its digits.
digit = 123
digits = digit.to_s.chars.map(&:to_i)
puts digits.inspect
And if you want to reverse the array, you can use the reverse
method.
digit = 123
digits = digit.to_s.chars.reverse.map(&:to_i)
puts digits.inspect