method

to_sentence

v4.2.9 - Show latest stable - Class: Array
to_sentence(options = {})
public

Converts the array to a comma-separated sentence where the last element is joined by the connector word.

You can pass the following options to change the default behavior. If you pass an option key that doesn’t exist in the list below, it will raise an ArgumentError.

Options

  • :words_connector - The sign or word used to join the elements in arrays with two or more elements (default: “, ”).

  • :two_words_connector - The sign or word used to join the elements in arrays with two elements (default: “ and ”).

  • :last_word_connector - The sign or word used to join the last element in arrays with three or more elements (default: “, and ”).

  • :locale - If i18n is available, you can set a locale and use the connector options defined on the ‘support.array’ namespace in the corresponding dictionary file.

Examples

[].to_sentence                      # => ""
['one'].to_sentence                 # => "one"
['one', 'two'].to_sentence          # => "one and two"
['one', 'two', 'three'].to_sentence # => "one, two, and three"

['one', 'two'].to_sentence(passing: 'invalid option')
# => ArgumentError: Unknown key :passing

['one', 'two'].to_sentence(two_words_connector: '-')
# => "one-two"

['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
# => "one or two or at least three"

Using :locale option:

# Given this locale dictionary:
#
#   es:
#     support:
#       array:
#         words_connector: " o "
#         two_words_connector: " y "
#         last_word_connector: " o al menos "

['uno', 'dos'].to_sentence(locale: :es)
# => "uno y dos"

['uno', 'dos', 'tres'].to_sentence(locale: :es)
# => "uno o dos o al menos tres"

5Notes

example

francois · Dec 17, 20112 thanks

['one','two','three'].to_sentence

=> "one, two, and three"

Sometimes, you need the "Oxford comma"

rab · Apr 1, 20142 thanks

Re: Gramatical error

http://imgur.com/fycHx

=== We invited the strippers, JFK, and Stalin.

versus the appositive phrase:

=== We invited the strippers, JFK and Stalin.

(Really, you need to see the comic to appreciate the difference.)

Grammatical error

rajivekjain · Apr 1, 2014

Hi - not sure where I would submit this so just putting here. My apologies if not in the right place.

default: “, and ” - this is grammatically wrong. There should be no comma with the last and.

Example:

['one', 'two', 'three'].to_sentence

should give: "one, two and three"

There is no ' .... , and ' which is considered grammatically incorrect I feel. The 'and' does it's job in the English language quite well by joining the two words it's in between.

Thank you.

No grammatical error

marnen · Mar 17, 2016

rajivekjain:

"A, B, and C" is correct in English, and is preferred by most style manuals. "A, B and C" is also correct in English. See https://en.wikipedia.org/wiki/Serial_comma

to_sentence_exclusive

joshuapinter · May 28, 2019

By default, +to_sentence+ combines elements inclusively by using ", and ".

If you want to be exclusive and combine the elements using ", or ", you can either pass in lengthy options to +to_sentence+ or use this handy extension I made to add +to_sentence_exclusive+ to the +Array+ class.

class Array

# Adds a simple method that overloads `to_sentence` except it uses "or" instead of "and", which is the default.
# The reason for this is because `to_sentence` is extremely flexible but that results in a lot of code to get
# the simple result of using "or" instead of "and". This makes it simple.
#
def to_sentence_exclusive
  self.to_sentence( two_words_connector: " or ", last_word_connector: ", or " )
end

end

Just drop this in +config/initializers/to_sentence_exclusive.rb+, restart your server or console and Bob's your uncle.