Turning long fixed number to array Ruby

前端 未结 7 672
我寻月下人不归
我寻月下人不归 2020-12-03 22:43

Is there a method in ruby to turn fixnum like 74239 into an array like [7,4,2,3,9]?

相关标签:
7条回答
  • 2020-12-03 23:19

    You can convert to string and use the chars method:

    74239.to_s.chars.map(&:to_i)
    

    Output:

    [7, 4, 2, 3, 9]
    

    Its a bit more elegant than splitting.

    0 讨论(0)
提交回复
热议问题