From: Alexandru Popescu Date: 2005-08-25T03:13:57+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 8:07 PM :# > Alexandru Popescu wrote: > >> I have the following hypothesis: a piece of text containing anywhere a line >> with the following content: >> >> [code] >> tags: tagone tagtwo >> [/code] >> >> with any number of tagnames on that line. >> >> Finally I want to modify the above text where every tagname is replaced by >> [[tagname => tagname]] > >> def prepare_content(new_content) >> updated_content = "" >> new_content.each_line do |line| >> if line !~ /^tags: /i >> updated_content += line >> next >> end >> >> tokens = line.split(%r{\s+}) >> depth = 0 >> 1.upto(tokens.length - 1) do |index| >> if tokens[index][0, 2] == "[[" >> depth += 1 >> next >> end >> if tokens[index][-2, 2] == "]]" >> depth -= 1 >> next >> end >> >> if depth == 0 >> page.tags << tokens[index] >> regexp = Regexp.new( '\s+(' + tokens[index] + ')\s*') >> line.sub!(regexp, ' [[\1 => \1]] ') >> end >> end >> end >> >> new_content += line >> end > > 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, Yes, I know, this is why I am asking to see how should I write it the Ruby way :-). I have posted again the spec as it seems I haven't succeed to be clear from the beginning. Sorry. :alex |.::the_mindstorm::.|