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 them with you:
Each next word begins with a capital letter:
"admin_user".camelize #=> "AdminUser"
Each space is replaced by an underscore (_
) character. The reverse of .camelize
:
"AdminUser".underscore #=> "admin_user"
Replaces underscores with dashes in the string:
"admin_user".dasherize #=> "admin-user"
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"
The reverse of .pluralize
, returns the singular form of the word in a string:
"users".singularize #=> "user"
"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"
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"
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"
Creates a class name from a plural table name:
"user".classify #=> "User"
"apple_and_bananas".classify #=> "AppleAndBanana"
Creates a name of the table for models:
"User".tableize #=> "users"
"AppleAndBanana".tableize #=> "apple_and_bananas"
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."John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"
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)"Product".demodulize # => "Product"
"Backoffice::UsersController".demodulize # => "UsersController"
"Admin::Hotel::ReservationUtils".demodulize # => "ReservationUtils"
"::Inflections".demodulize # => "Inflections"
"".demodulize # => ""
deconstantize
does the opposite - it captures the path to the constant without the constant name (top level constants return an empty string)"Product".deconstantize # => ""
"Backoffice::UsersController".deconstantize # => "Backoffice"
"Admin::Hotel::ReservationUtils".deconstantize # => "Admin::Hotel"
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 as tableize
in ActiveRecord's internals"User".foreign_key # => "user_id"
"InvoiceLine".foreign_key # => "invoice_line_id"
"Admin::Session".foreign_key # => "session_id"
Another method not found in the guide:
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.
'what a Lovely Day'.upcase_first # => "What a Lovely Day"
Refer: https://dev.to/junko911/rails-helper-methods-to-change-the-form-of-strings-1h9c