I am converting URLs in a text file to hyperlinks with the following 
regex. But the results only show up after the gsub is run a second time.

str = "A link to http://ruby-lang.org"
str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
"#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
#=> "A link to<a href='' target='_blank'></a>"
str.gsub(/([\s|\>|\[|\(])((ftp:\/\/|http(s?):\/\/))([\w\.\?\/&=\-~:%]+)\b/i,
"#{$1}<a href='#{$3}#{$5}#{$6}' target='_blank'>#{$5}#{$6}</a>")
#=> "A link to <a href='http://ruby-lang.org' 
target='_blank'>ruby-lang.org</a>"

Is there another way to do this?