On Thu, Oct 11, 2012 at 8:14 PM, Anna Baas <lists / ruby-forum.com> wrote: > Hi, > > I've tried searching for this on the forum and couldn't find an answer. > Apologies if it's already out there. > > I've converted the digits of an integer into elements of an array, like > so: > > x = some_integer > > y = "#{x}".scan(/./) Better y = x.to_s.scan /./ You can also do this numerically irb(main):008:0> x = 113453 => 113453 irb(main):009:0> y = [] => [] irb(main):010:0> while x > 0; x, b = x.divmod 10; y.unshift b; end => nil irb(main):011:0> y => [1, 1, 3, 4, 5, 3] respectively irb(main):015:0> x = 113453 => 113453 irb(main):016:0> y = [] => [] irb(main):017:0> while x > 0; x, b = x.divmod 10; y.unshift b.to_s; end => nil irb(main):018:0> y => ["1", "1", "3", "4", "5", "3"] or even irb(main):030:0> x = 113453 => 113453 irb(main):031:0> y = [] => [] irb(main):032:0> while x > 0; y.unshift((x, b = x.divmod 10).last.to_s); end => nil irb(main):033:0> y => ["1", "1", "3", "4", "5", "3"] > Now, after using y for my nefarious purposes, I would like to re-convert > the elements (still containing one digit each) into an integer. You make me curious... > I don't mind writing a routine for it, but was wondering if there's some > magical way to do it quicker :) No magic needed z = y.join.to_i Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/