How to split a number into an array of its digits?
123
should become [1,2,3]
I found this thread in the Ruby forum when I was looking for an idiomatic solution to the question above. The solutions there are working for me and they seem fine, I am using .to_s.chars.map(&:to_i)
But the thread is quite old, and I want to known if there are better, maybe newer ways to solve the task, given that Ruby as a language has developed further?
Known solutions:
.to_s.chars.map(&:to_i)
12345.to_s.scan(/./)
How do you solve it? Why? To me as a beginner, seeing different ways to solve a problem helps a lot to understand the language and its use better. So, thanks for your input!