Skip to content

Instantly share code, notes, and snippets.

@barelyknown
Created September 17, 2012 21:01
Show Gist options
  • Save barelyknown/3739741 to your computer and use it in GitHub Desktop.
Save barelyknown/3739741 to your computer and use it in GitHub Desktop.
space_case method for strings to humanize camelcase class names
class String
# Change "HelloWorld" into "Hello World" while keeping "HELLOWORLD" as "HELLOWORLD"
# Useful as an alternative to humanize when presenting a list of class names
def space_case
split(/((?<=[a-z])[A-Z])/).inject { |whole, part| whole += (part =~ /^[A-Z]$/) ? " #{part}" : part }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment