Module: ArrayExtensions
- Defined in:
- lib/multi_rails/core_extensions.rb
Instance Method Summary collapse
-
#to_sentence(options = {}) ⇒ Object
Converts the array to comma-seperated sentence where the last element is joined by the connector word.
Instance Method Details
#to_sentence(options = {}) ⇒ Object
Converts the array to comma-seperated 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”.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/multi_rails/core_extensions.rb', line 26 def to_sentence( = {}) .reverse_merge! :connector => 'and', :skip_last_comma => false case length when 0 "" when 1 self[0] when 2 "#{self[0]} #{[:connector]} #{self[1]}" else "#{self[0...-1].join(', ')}#{[:skip_last_comma] ? '' : ','} #{[:connector]} #{self[-1]}" end end |