From: Jim Menard Date: 2005-08-25T02:59:22+09:00 Subject: Re: ruby way for piece of code 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 -- Jim Menard, jim.menard@gmail.com, jimm@io.com http://www.io.com/~jimm