From: Alexandru Popescu Date: 2005-08-25T02:07:16+09:00 Subject: ruby way for piece of code Hi! 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]] This is the code I have done, but I feel it is not the rubyiest: [code] 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 [/code] Note: the above code is able also to avoid the case where a tagname was already transformed into [[tagname => tagname]] many thanks for any ruby ideas, :alex |.::the_mindstorm::.|