From: Alexandru Popescu Date: 2005-08-25T05:34:10+09:00 Subject: Re: ruby way for piece of code #: Nikolai Weibull changed the world a bit at a time by saying on 8/24/2005 10:09 PM :# > Alexandru Popescu wrote: > >> > def prepare_content(new_content) >> > updated_content = "" >> > new_content.each do |line| >> > if line =~ /^tags:\s+/i >> > $'.split(/\s+/).each{ |t| updated_content << " [[#{t} => #{t}]]" } >> > updated_content << "\n" >> > else >> > updated_content << line >> > end >> > end >> > updated_content >> > end >> > >> > your code is messy, > >> I have posted again the spec as it seems I haven't succeed to be clear >> from the beginning. Sorry. > > OK, so how about the following then: > > def prepare_content(new_content) > updated_content = "" > new_content.each do |line| > if line =~ /^tags:\s+/i > $'.split(/\s+/).each do |token| > if token =~ /\[\[.* => .*\]\]/ > updated_content << " " << token > else > updated_content << " [[#{t} => #{t}]]" > end > end > updated_content << "\n" > else > updated_content << line > end > end > updated_content > end > > (Why, and how?, are you processing the document for tag-expansion > multiple times in this way?), > nikolai > many thanks! it looks like Ruby now :-). why and how: Imagine it is f.e. a wiki page to which you are adding tags when you edit it, so just before saving it you can reach the situation where you have tags: a [[b => b]] c :alex |.::the_mindstorm::.|