Enables to conversion of Arrays to human readable lists. [‘one’, ‘two’, ‘three’] => "one, two, and three"
Methods
Public Instance methods
When an array is given to url_for, it is converted to a slash separated string.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 24
24: def to_param
25: join '/'
26: end
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 more than two elements (default: "and")
- :skip_last_comma: Set to true to return "a, b and c" instead of "a, b, and c".
[ show source ]
# File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 9
9: def to_sentence(options = {})
10: options.assert_valid_keys(:connector, :skip_last_comma)
11: options.reverse_merge! :connector => 'and', :skip_last_comma => false
12:
13: case length
14: when 1
15: self[0]
16: when 2
17: "#{self[0]} #{options[:connector]} #{self[1]}"
18: else
19: "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]} #{self[-1]}"
20: end
21: end