This method changes all URLs in the string into an HTML link. The goal is to not mutate the string passed in, which I believe is a good practice.
The new_string
code in this method does not seem idiomatic.
def autolink_string string
return if string.nil?
return '' if string.empty?
regex = /(\S+\.(com|net|org|edu|gov)(\/\S+)?)/
groups = string.scan regex
matches = groups.map &:first
new_string = string.clone
matches.each do |match|
new_string.gsub! match, "<a href='http://#{ match }'>#{ match }</a>"
end
new_string
end
How can I refactor to remove the new_string
? Any other suggestions?