Skip to content

Instantly share code, notes, and snippets.

@josephbridgwaterrowe
Created December 21, 2014 22:08
Show Gist options
  • Save josephbridgwaterrowe/3fca3f52c73d75c0bf41 to your computer and use it in GitHub Desktop.
Save josephbridgwaterrowe/3fca3f52c73d75c0bf41 to your computer and use it in GitHub Desktop.
String refinements
module StringExtensions
refine String do
# "Brute force" underscore the string.
# Removes any non-alpha characters for clean symbol ready strings.
def brute_underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
gsub(/([\\][\"])/,'').
tr('-', '_').
tr(' ', '_').
squeeze('_').
downcase
end
end
end
@josephbridgwaterrowe
Copy link
Author

I'm sure that this doesn't really do everything it claims to yet, but I will add the functionality once I identify its failings... I also need to add some tests and perhaps put this into a Gem/repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment