If you have a site where users can send content, and they often send emails on it, it’s good to protect them from spam bots.

Here follows a simple helper method that uses a javascript technique to do that.

def hide_emails_from_bots(text)
    match = text.match(/\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/).to_s
    if match.blank?
      text
    else
      size = match.size
      splitter = size / 3
      string1 = match[0...splitter]
      string2 = match[splitter...2*splitter]
      string3 = match[2*splitter..size]
      js = "
        <script type="\&quot;text/javascript\&quot;"><!--mce:0--></script>
      "
      text.gsub(/#{match}/, js)
    end
  end