Changing URLs into HTML links
You can use gsub! and block directly with a clone.

def autolink_string str
  unless str.empty?
    regex = /(\S+\.(com|net|org|edu|gov)(\/\S+)?)/
    str.clone.gsub!(regex) do |match|
      "&lt;a href='http://#{ match }'&gt;#{ match }&lt;/a&gt;"
    end || str # Return str if no match
  end
end


In console:

&gt; str = "this is a test abc.com"
&gt; result = autolink_string str
=&gt; "this is a test &lt;a href='http://abc.com'&gt;abc.com&lt;/a&gt;"
&gt; str
=&gt; "this is a test abc.com"
&gt; result
=&gt; "this is a test &lt;a href='http://abc.com'&gt;abc.com&lt;/a&gt;"

&gt; autolink_string "no url here"
=&gt; "no url here"

&gt; autolink_string ""
=&gt; ""

&gt; autolink_string "this is a test abc.com and test twitter.com"
=&gt; "this is a test &lt;a href='http://abc.com'&gt;abc.com&lt;/a&gt; and test &lt;a href='http://twitter.com'&gt;twitter.com&lt;/a&gt;"