Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/meroku/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#to_sentence(options = {}) ⇒ Object

File activesupport/lib/active_support/core_ext/array/conversions.rb, line 59



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/meroku/core_ext.rb', line 3

def to_sentence(options = {})
  options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)

  default_connectors = {
    :words_connector     => ', ',
    :two_words_connector => ' and ',
    :last_word_connector => ', and '
  }
  if defined?(I18n)
    i18n_connectors = I18n.translate(:'support.array', locale: options[:locale], default: {})
    default_connectors.merge!(i18n_connectors)
  end
  options = default_connectors.merge!(options)

  case length
  when 0
    ''
  when 1
    self[0].to_s.dup
  when 2
    "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
  else
    "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
  end
end