Recently I’ve been making my first custom library at work, and I’ve needed ways to turn strings into classes and classes into strings, I found some helpful methods for doing so by digging around a bit!

Humanize

# This will return "Hello world"
"hello_world".humanize

Constantize

# This will return Editor::Template
"Editor::Template".constantize

Underscore

# This will return "hello_world"
"HelloWorld".underscore

Camelize

# This will return "HelloWorld"
"hello_world".camelize

Parameterize

# This will return "hello-world"
"Hello World".parameterize

Published on Tuesday, 28 June 2011