method

to_sentence

Importance_2
v2.0.3 - 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:

  • :connector - The word used to join the last element in arrays with two or more elements (default: "and")
  • :skip_last_comma - Set to true to return "a, b and c" instead of "a, b, and c".
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"