method

to_sentence

Importance_2
v2.3.8 - Show latest stable - 1 note - Class: ActiveSupport::CoreExtensions::Array::Conversions
to_sentence(options = {}) public

Converts the array to a comma-separated sentence where the last element is joined by the connector word. 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 ")
Show source
Register or log in to add new notes.
March 2, 2009
2 thanks

Valid options changed in Rails v2.3.0 RC1

The valid options in Rails v2.3.0 RC1 are no longer

:connector and :skip_last_comma, but

:words_connector, :two_words_connector, and :last_word_connector:

>> %w(lorem ipsum dolor sit).to_sentence
=> "lorem, ipsum, dolor, and sit"

>> %w(lorem ipsum dolor sit).to_sentence(:words_connector => ' + ')
=> "lorem + ipsum + dolor, and sit"

>> %w(lorem ipsum).to_sentence(:two_words_connector => ' through ')
=> "lorem through ipsum"

# No effect if more than two words
>> %w(lorem ipsum dolor sit).to_sentence(:two_words_connector => ' through ')
=> "lorem, ipsum, dolor, and sit"

>> %w(lorem ipsum dolor sit).to_sentence(:last_word_connector => ' or ')
=> "lorem, ipsum, dolor or sit"