method
to_sentence
v2.0.3 -
Show latest stable
-
1 note -
Class: ActiveSupport::CoreExtensions::Array::Conversions
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (1)
- 2.3.8 (13)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
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".
Register or
log in
to add new notes.
zoopzoop -
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"