From: "Kroeger Simon (ext)" Date: 2005-09-06T17:14:20+09:00 Subject: Re: [Q] how to do insertion and smooth traversal simultaneously on array as Robert said: ----------------------------------------------------------------- a = [ 0, 1, 2, 3, 4, 5 ] actions = [] a.each_with_index do |x, i| puts "[#{i}] : #{x}" if i % 2 == 0 # even index ? actions << [a, :insert, [i, ':-)']] end end # do it! actions.reverse.each{|action| action[0].send(action[1], *action[2])} puts a.size ------------------------------------------------------------------ [0] : 0 [1] : 1 [2] : 2 [3] : 3 [4] : 4 [5] : 5 9 cheers Simon > -----Original Message----- > From: SHIGETOMI, Takuhiko [mailto:tshiget1@gw.nsw.co.jp] > Sent: Tuesday, September 06, 2005 9:32 AM > To: ruby-talk ML > Cc: tshiget1@gw.nsw.co.jp > Subject: [Q] how to do insertion and smooth traversal > simultaneously on array > > greetings, guys. > > this is a pseudo-ruby code which shows what i am expecting. > > a = [ 0, 1, 2, 3, 4, 5 ] > a.each_with_index do |x, i| > puts "[#{i}] : #{x}" > if i % 2 == 0 # even index ? > a.insert( i, ':-)' ) > skip 1 # <- this is a pseudo directive to skip the next element > # which has just been inserted now > end > end > puts a.size > > expected output... > > [0] : 0 > [1] : 1 > [2] : 2 > [3] : 3 > [4] : 4 > [5] : 5 > 9 <-- means that 3 smileys have been inserted > > as above, i want to traverse an array with some conditional > insertion to > itself. > > do anyone know cool way for that? > > ttsp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/ > 3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618 > > >