Skip to content

Instantly share code, notes, and snippets.

@huned
Created June 25, 2009 00:03
Show Gist options
  • Select an option

  • Save huned/135601 to your computer and use it in GitHub Desktop.

Select an option

Save huned/135601 to your computer and use it in GitHub Desktop.
String#emphasize puts <em>'s in strings
class String
def emphasize substrings
cleaned = [substrings].flatten.compact.uniq.map(&:strip).reject(&:empty?)
return self.clone if cleaned.empty?
returning to_s do |em|
cleaned.inject em do |s, sub|
s.gsub!(%r/(#{sub})/i, '<em>\1</em>') || s
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment