Module: Nokogiri::XML::Schematron::Internal::CoreExt::Array

Defined in:
lib/nokogiri/xml/schematron/internal/core_ext/array.rb

Class Method Summary collapse

Class Method Details

.to_sentence(array, **options) ⇒ String

Converts the array to a comma-separated sentence where the last element is joined by the connector word.

Parameters:

  • array (Array<String>)

    the array of strings.

  • options (Hash<Symbol, Object>)

    the options.

Options Hash (**options):

  • :words_connector (String)

    the sign or word used to join the elements in arrays with two or more elements.

  • :two_words_connector (String)

    the sign or word used to join the elements in arrays with two elements.

  • :last_word_connector (String)

    the sign or word used to join the last element in arrays with three or more elements.

Returns:

  • (String)

    the comma-separated sentence.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nokogiri/xml/schematron/internal/core_ext/array.rb', line 16

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