So I needed to remove tags <br />from the beginning and end of lines in the project I'm working on. I made a small little method that does what I need, but I'm not sure if this is the best way to do such things. I suspect there might be a handy regex that I can use to do this in only a few lines. Here is what I got:
def remove_breaks(text)
if text != nil and text != ""
text.strip!
index = text.rindex("<br />")
while index != nil and index == text.length - 6
text = text[0, text.length - 6]
text.strip!
index = text.rindex("<br />")
end
text.strip!
index = text.index("<br />")
while index != nil and index == 0
text = test[6, text.length]
text.strip!
index = text.index("<br />")
end
end
return text
end
Now it "<br />"really can be anything, and it would probably be more useful to make a general use function that takes as an argument a string that should be removed from the beginning and the end.
I am open to any suggestions on how to make it cleaner, because it just seems that it can be improved.