Rails has a lot of built-in helper methods that do not just help you write readable/clean code but also provide you with a great coding experience and this is one of the reasons why Rails is so popular. I have created a list of Rails helper methods for changing the form of strings and would like to share with you:
Camel Case
Each next word begins with a capital letter:
"admin_user".camelize #=> "AdminUser"
Snake case
Each space is replaced by an underscore (_
) character. The reverse of .camelize
:
"AdminUser".underscore #=> "admin_user"
Dasherize
Replaces underscores with dashes in the string:
"admin_user".dasherize #=> "admin-user"
Pluralize
Returns the plural form of the word in the string:
"user".pluralize #=> "users"
"person".pluralize #=> "people"
"fish".pluralize #=> "fish"
"octopus".pluralize #=> "octopi"
"apple and banana".pluralize #=> "apple and bananas"
"apple_and_banana".pluralize #=> "apple_and_bananas"
"apple-and-banana".pluralize #=> "apple-and-bananas"
"AppleAndBanana".pluralize #=> "AppleAndBananas"
Singularize
The reverse of .pluralize
, returns the singular form of the word in a string:
"users".singularize #=> "users"
"people".singularize #=> "person"
"apples and bananas".singularize #=> "apples and banana"
"apples_and_bananas".singularize #=> "apples_and_banana"
"apples-and-bananas".singularize #=> "apples-and-banana"
"ApplesAndBananas".singularize #=> "ApplesAndBanana"
Titleize
Capitalizes all the words and replaces some characters in the string to create a nicer looking title:
"the lord of the rings".titleize #=> "The Lord Of The Rings"
Humanize
Capitalizes the first word, turns underscores into spaces, and strips a trailing _id
if present. Like .titleize
, this is meant for creating pretty output:
"apples_and_bananas".humanize #=> "Apples and bananas"
"user_id".humanize #=> "User"
Classify
Creates a class name from a plural table name:
"user".classify #=> "User"
"apple_and_bananas".classify #=> "AppleAndBanana"
Tableize
Creates a name of the table for models:
"User".tableize #=> "users"
"AppleAndBanana".tableize #=> "apple_and_bananas"
If you know any other helper methods, please let me know in the comment!
Top comments (3)
Great write-up! I've seen the output of
dasherize
half-jokingly called "kebab-case" to contrast from "camelCase" or "snake_case", but it's pretty uncommon outside of Clojure and other lisps to use it often.There are a few more string inflection methods listed in the guide but they are probably more likely to be used in backend code or meta-programming than in the front end (I could be wrong!). I think all of these (apart from parameterize) expect a constant name like a class name or module name, so you're dealing with transformations to strings that are expected to be code and not arbitrary input.
parameterize
turns a string into a url-safe slug/path-component.demodulize
returns the part of a class/constant that's local to the namespace (it removes all the module information and just returns the last part of the name)deconstantize
does the opposite - it captures the path to the constant without the constant name (top level constants return an empty string)foreign_key
is used to turn an associated model class into a default column name to find the id - this probably is used in the same layer astableize
in ActiveRecord's internalsThank you, Daniel! I wasn't aware of those. Awesome!
Another method not found in the guide:
Upcase First
Similar to Humanize, but much simpler without the additional processing that may not be intended in some instances. Converts just the first character to uppercase.
Refer: api.rubyonrails.org/classes/String...