skip to content

In Rails, how do Array#to_sentence and String inflections such as pluralize, tableize and parameterize turn guest data into readable text?

level: juniorimportance: should knowfreq 38%

answer

  1. ", " and ", and " connectors
  2. support.array keys in the locale file
  3. pluralize(count) returns only the word
  4. camelize, underscore, tableize, classify
  5. parameterize for URL slugs

basics

~10 s

to_sentence joins an array as "Ana, Ben, and Cy" with configurable, I18n-aware connectors. String inflections rewrite words: pluralize(count), singularize, camelize, underscore, tableize, classify, humanize, titleize and parameterize for slugs.

solid answer

~40 s

`Array#to_sentence` joins elements with three connectors: `words_connector` (`", "`), `two_words_connector` (`" and "`) and `last_word_connector` (`", and "`), so three guests read "Ana, Ben, and Cy" and two read "Ana and Ben". The defaults come from the `support.array` keys of the current locale, you can override any of them, and an unknown key raises `ArgumentError`. `String#pluralize(count)` returns the word alone, singular when `count == 1`: `"guest".pluralize(3)` is `"guests"`, and irregulars such as `"person"` become `"people"`. The naming inflections are the ones Rails itself uses: `"EventInvitation".tableize` is `"event_invitations"`, `classify` reverses it, `camelize` and `underscore` convert between class and file names, and `parameterize` builds URL slugs.

code

ruby · 11 lines
ruby
%w[Ana Ben Cy].to_sentence                    # => "Ana, Ben, and Cy"
%w[Ana Ben].to_sentence                       # => "Ana and Ben"
%w[Ana Ben Cy].to_sentence(last_word_connector: " or ")
# => "Ana, Ben or Cy"
%w[Ana Ben].to_sentence(locale: :de)          # uses support.array from de.yml

"guest".pluralize(1)   # => "guest"
"guest".pluralize(0)   # => "guests"
"person".pluralize     # => "people"
"EventInvitation".tableize # => "event_invitations"
"Summer Garden Party 2026".parameterize # => "summer-garden-party-2026"

go deeper

for a junior

Recall what to_sentence returns for one, two and three items, and that pluralize with a count picks singular only for 1.

for a middle

Explain the three connectors and their locale keys, the naming inflections Rails uses for tables and classes, and parameterize for slugs.

for a senior

Know the limits: English-only rules, acronyms that need configuration, and the html-safe to_sentence helper for joining links in views.

for a principal

Decide when display text should leave inflections for I18n plural rules, so copy and languages can change without code.

## Building the guest-list sentence An invitation page often needs a line such as "Ana, Ben, and Cy are coming". **Active Support** adds `Array#to_sentence` for this. It handles every length: | Array | `to_sentence` | |---|---| | `[]` | `""` | | `["Ana"]` | `"Ana"` | | `["Ana", "Ben"]` | `"Ana and Ben"` | | `["Ana", "Ben", "Cy"]` | `"Ana, Ben, and Cy"` | Three options control the joins: - `words_connector:` between all but the last two elements, default `", "`; - `two_words_connector:` when there are exactly two, default `" and "`; - `last_word_connector:` before the last of three or more, default `", and "`. `["Ana", "Ben", "Cy"].to_sentence(last_word_connector: " or ")` gives "Ana, Ben or Cy". The method validates its keys with `assert_valid_keys`, so a misspelled option raises `ArgumentError` rather than being ignored. ## Localized connectors In practice the connectors come from the locale: when I18n is loaded, `to_sentence` reads `support.array.words_connector`, `two_words_connector` and `last_word_connector` from the current locale and lets explicit options override them. A German locale file can therefore turn the same call into "Ana, Ben und Cy" without touching the view. `locale: :de` picks a locale for one call, and `locale: false` skips I18n. In a template, Action View also offers a `to_sentence(array)` helper, which escapes each element and returns an HTML-safe string, so a list of `link_to` tags can be joined without marking user-supplied names as safe by hand. ## Counting words: `pluralize` `String#pluralize` returns the plural form, using the inflection rules (`"person"` to `"people"`, `"sheep"` unchanged). Given a count, it returns the singular only when `count == 1`: ```ruby guests = event.guests.pluck(:name) "#{guests.to_sentence} #{guests.size == 1 ? 'is' : 'are'} coming " \ "(#{guests.size} #{'guest'.pluralize(guests.size)})" # => "Ana, Ben, and Cy are coming (3 guests)" ``` Note that `String#pluralize(3)` returns just `"guests"`; the number is not included. The view helper `pluralize(3, "guest")` is a different method from Action View and returns `"3 guests"`. For translated text, plural rules belong in locale files rather than in English-only inflections. ## The naming inflections The same inflector drives Rails' own conventions, which is why interviewers ask about it: | Method | Example | Result | |---|---|---| | `camelize` | `"event_invitation"` | `"EventInvitation"` | | `underscore` | `"EventInvitation"` | `"event_invitation"` | | `tableize` | `"EventInvitation"` | `"event_invitations"` | | `classify` | `"event_invitations"` | `"EventInvitation"` | | `foreign_key` | `"Event"` | `"event_id"` | | `humanize` | `"rsvp_due_date"` | `"Rsvp due date"` | | `titleize` | `"rsvp_due_date"` | `"Rsvp Due Date"` | | `parameterize` | `"Summer Garden Party 2026"` | `"summer-garden-party-2026"` | `parameterize` lowercases, transliterates accented characters and replaces anything that is not a letter, digit, dash or underscore with the separator (`separator: "-"` by default, `preserve_case: false`), which makes it the usual tool for a `to_param` slug. `humanize` also drops a trailing `_id`. ## Where Rails uses these inflections itself The inflector is not just for display text. Rails' naming conventions are computed with it: - a model `EventInvitation` maps to the table `event_invitations` (`tableize`) and a `has_many :event_invitations` association finds that class by singularizing and camelizing its name; - `belongs_to :event` expects the column `event_id` (`foreign_key`); - the file `event_invitation.rb` holds `EventInvitation` (`underscore` and `camelize`), which is the mapping the autoloader relies on; - form labels and error messages default to `humanize` of the attribute name. That is why changing an inflection rule can change table names and constant lookup, not just page copy. ## Limits worth knowing 1. The built-in rules are English. `pluralize(:es)` uses Spanish rules only if the app defines them, and acronyms such as "RSVP" need an acronym rule in the inflections initializer, or `humanize` keeps writing "Rsvp". 2. Inflections are conventions, not grammar: an irregular word missing from the rules pluralizes wrongly until you add it. 3. `to_sentence` returns a plain `String`. Output through `<%= %>` is escaped, which is safe; marking it `html_safe` after joining user names is not.

  • How would you make the guest sentence read "Ana, Ben und Cy" for German visitors?
    Define `support.array.words_connector`, `two_words_connector` (`" und "`) and `last_word_connector` (`" und "`) in `config/locales/de.yml`. `to_sentence` reads those keys for the current locale, so the same call localizes without options in the view.
  • When would you use Action View's `to_sentence(array)` helper instead of `Array#to_sentence` in a template?
    When the elements are HTML, such as `link_to` tags for each guest. The helper escapes every element that is not already HTML-safe and returns an HTML-safe string, so links survive and user-typed names stay escaped. `Array#to_sentence` returns a plain string, which `<%= %>` escapes as a whole, links included.

  • String#pluralize(3) returns "3 guests" with the number included.
  • to_sentence ignores unknown options like any keyword hash would.
  • to_sentence always uses English connectors whatever the locale.
  • tableize returns the class name, so "event_invitations".tableize gives EventInvitation.
  • Inflections understand English grammar, so every irregular word works out of the box.