ruby way for piece of code

A

Alexandru Popescu

Hi!

I have the following hypothesis: a piece of text containing anywhere a line with the following content:

Code:
tags: tagone tagtwo

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

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::.|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top