Skip to content

Instantly share code, notes, and snippets.

@mxlje
Created July 12, 2013 10:02
Show Gist options
  • Save mxlje/5983297 to your computer and use it in GitHub Desktop.
Save mxlje/5983297 to your computer and use it in GitHub Desktop.
Ruby method for cleaning URL strings for display
class String
# remove protocol, www, and trailing slash from URL string
def to_clean_url
u = self
if u.start_with?('http://www.')
u = u[11..-1]
elsif u.start_with?('https://www.')
u = u[12..-1]
elsif u.start_with?('http://')
u = u[7..-1]
elsif u.start_with?('https://')
u = u[8..-1]
end
if u.end_with?('/')
u = u.chomp('/')
end
u
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment