Turning long fixed number to array Ruby

前端 未结 7 758
我寻月下人不归
我寻月下人不归 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 22:56

    You can also use Array.new instead of map:

    n = 74239
    
    s = Math.log10(n).to_i + 1 # Gets the size of n
    
    Array.new(s) { d = n % 10; n = n / 10; d }.reverse 
    

提交回复
热议问题