From: Ara.T.Howard@... Date: 2004-11-11T08:13:31+09:00 Subject: Re: array.each restart when array is changed --8323328-1522950913-1100127762=:14646 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1522950913-1100127762=:14646" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1522950913-1100127762=:14646 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Wed, 10 Nov 2004, Kevin [ISO-8859-15] B=F6rgens wrote: > Hi! > > > How to restart a "each" iteration when the iterated array is changed? I d= o > it like this > > changed=3Dfalse > while (changed) > changed=3Dfalse > array.each {|e| > if (today_is_christmas) > array=3Darray+=3D["Hello santa claus"] > changed=3Dtrue > end > } > > Is there a more elegant way to do this? > > > Second question: > why is this not allowed > > #show me the first n letters of the alphabet > ('a'...'a'+gets.to_i).each{|e| puts e } > > How to do it the right way? > > Thanks in advance, > Kevin maybe: harp:~ > cat a.rb bits =3D [0, 1, 0, 1] n =3D 0 begin bits.each_with_index do |bit, pos| n |=3D (bit << pos) bits << 0 << 1 and raise 'changed' if bits.size =3D=3D 4 and pos =3D= =3D 3 end rescue =3D> e retry end p n printf "%b\n", n harp:~ > ruby a.rb 42 101010 i don't know if changing an array while iterating is safe though - i'll def= er to someone else on that note. cheers. -a -- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-1522950913-1100127762=:14646-- --8323328-1522950913-1100127762=:14646--