Rails String InflectionsVote on HN Tweet
I've been using constantize to turn strings into Class objects. Constantize is mixed into the String class by Rails. This got me to thinking about other clever helpers that might be mixed into the string class. A quick search through the API did not disappoint.
In ActiveSupport::CoreExtensions::String::Inflections, I found a whole list of goodies. I can find the database table name for a given class with 'tableize'. If I just wanted a Class name string instead of a Class object, I can use 'classify'. 'dasherize' probably isn't too useful for me, but I thought it was cute. I can see 'demodulerize' and 'foreign_key' to be very useful. 'humanize', 'pluralize', 'titleize' could be really useful for manipulating text before it's rendered for the user. 'underscore' can be used to calculate paths to files based on their class names.
Here's a quick cheatsheet of all the methods.
|camelcase||same as camelize|
|constantize||classify, then turns it into a class object|
|titlecase||same as titleize|
|titleize||good night moon||Good Night Moon|