From: Alexandru Popescu Date: 2005-08-25T03:06:58+09:00 Subject: Re: ruby way for piece of code #: Jim Menard changed the world a bit at a time by saying on 8/24/2005 7:59 PM :# > On 8/24/05, Alexandru Popescu wrote: >> #: Jim Menard changed the world a bit at a time by saying on 8/24/2005 7:41 PM :# >> > The code that replaces >> > tag: a b c d b >> > with >> > tag: [[a => a]] [[b => b]] [[c => c]] [[d => d]] >> > could be written >> > >> > new_content << 'tag:' >> > line.split[1..-1].uniq.each { | tag | new_content << " [[#{tag} => #{tag}]]" } >> > >> > Jim >> >> Nice but it misses the case where one of the tags where already changed. > > Aah, alreadh changed on any previous line. Got it. > > done = [] # at the beginning, before any lines processed > > new_content << 'tag:' > tags = line.split[1..-1]..each { | tag | > unless done.include?(tag) > new_content << " [[#{tag} => #{tag}]]" > done << tag > end > } > > Jim Damn I hate my english sometimes :-(. I will try again: initially: case 1/ tags: a b c d case 2/ tags: a [[b => b]] c [[d => d]] The result should always be the same tags: [[a => a]] [[b => b]] [[c => c]] [[d => d]] Clarification: at a previous step some of them can already be changed from tags: a b c d to tags: a [[b => b]] c d hope this time my english was good enough to express my stupid problem :alex |.::the_mindstorm::.|