From: Robert Klemme Date: 2008-05-13T20:26:12+09:00 Subject: Re: Convert integer to array? 2008/5/12 Sebastian Hungerecker : > Nadim Kobeissi wrote: > > Let's say I have: > > x=1234 > > How can I convert that to the follow array: > > x=[1, 2, 3, 4] > > A solution that doesn't use strings: > > result_array = [] > while x > 0 > result_array.unshift x % 10 > x /= 10 > end > result_array > > This will "destroy" x, though. Well, that's easily fixed: just work with another variable. You can also combine division and mod: def int_split(x) r = [] y = x while y > 0 y, b = y.divmod 10 r.unshift b end r end Kind regards robert -- use.inject do |as, often| as.you_can - without end