DEV Community

loading...

Rails helper methods to change the form of strings

Junko T.
Software engineer - Ruby/Ruby on Rails, React, JS
・2 min read

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 with you:

Camel Case

Each next word begins with a capital letter:

"admin_user".camelize #=> "AdminUser"
Enter fullscreen mode Exit fullscreen mode

Snake case

Each space is replaced by an underscore (_) character. The reverse of .camelize:

"AdminUser".underscore #=> "admin_user"
Enter fullscreen mode Exit fullscreen mode

Dasherize

Replaces underscores with dashes in the string:

"admin_user".dasherize #=> "admin-user"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Classify

Creates a class name from a plural table name:

"user".classify #=> "User"
"apple_and_bananas".classify #=> "AppleAndBanana"
Enter fullscreen mode Exit fullscreen mode

Tableize

Creates a name of the table for models:

"User".tableize #=> "users"
"AppleAndBanana".tableize #=> "apple_and_bananas"
Enter fullscreen mode Exit fullscreen mode

If you know any other helper methods, please let me know in the comment!

Discussion (2)

Collapse
djuber profile image
Daniel Uber

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.
"John Smith".parameterize # => "john-smith"
"Kurt Gödel".parameterize # => "kurt-godel"
Enter fullscreen mode Exit fullscreen mode
  • 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                               # => ""
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode
Collapse
junko911 profile image
Junko T. Author

Thank you, Daniel! I wasn't aware of those. Awesome!