Blog #7

Rails helper methods to change the form of strings

DATPMT Jul 10 2023 Tag icon
#Rails
Rails logo


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:

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 #=> "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"

 

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"

 

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.

"John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"

 

"Product".demodulize                        # => "Product"
"Backoffice::UsersController".demodulize    # => "UsersController"
"Admin::Hotel::ReservationUtils".demodulize # => "ReservationUtils"
"::Inflections".demodulize                  # => "Inflections"
"".demodulize                               # => ""

 

"Product".deconstantize                        # => ""
"Backoffice::UsersController".deconstantize    # => "Backoffice"
"Admin::Hotel::ReservationUtils".deconstantize # => "Admin::Hotel"

 

"User".foreign_key           # => "user_id"
"InvoiceLine".foreign_key    # => "invoice_line_id"
"Admin::Session".foreign_key # => "session_id"

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.   
 

'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