From: Nikolai Weibull Date: 2005-08-25T03:07:32+09:00 Subject: Re: ruby way for piece of code 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 This should do what you want, provided that I understand your specification. I don't know where you get that page variable from, so I didn't include code for it; your code is messy, nikolai -- Nikolai Weibull: now available free of charge at http://bitwi.se/! Born in Chicago, IL USA; currently residing in Gothenburg, Sweden. main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}