ruby - Take a number and split it into indexed groups in a single dimensional array -


i have number 12,345,678 , i'm trying split array that's groups of 3. such if have:

stack = 12345678 

then following array:

overflow = [12,345,678] 

i thinking using enumberable#each_slice similar question asked here, creates 2d array. considered using array#flatten wouldn't keep numbers grouped properly.

is there nice way this, or going nasty nested loop? thx!

this may not efficient, works. can build on/optimize it.

 stack.to_s.chars.reverse.each_slice(3).map {|s| s.reverse.join.to_i }.reverse  # => [12, 345, 678] 

Comments