Module: English::Array

Included in:
Array
Defined in:
lib/gems/english-0.3.1/lib/english/array.rb

Instance Method Summary collapse

Instance Method Details

#conjunction(type = :and, seperator = ",") ⇒ Object Also known as: join_sentence

This is more advnaced form of #join. It allows for a different terminating separator.

The default type of conjunction –the terminating separator, is “and”, and the default regualt separator is “,”.

[1,2,3].conjunction
=> "1, 2 and 3

[1,2,3].conjunction(:or)
=> "1, 2 or 3

[1,2,3].conjunction(:or, ';')
=> "1; 2 or 3


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gems/english-0.3.1/lib/english/array.rb', line 20

def conjunction(type=:and, seperator=",")
  type      = type.to_s
  separator = separator.to_s
  space     = space.to_s

  case length
  when 0
    ""
  when 1
    self[0]
  when 2
    "#{self[0]} #{type} #{self[1]}"
  else
    [self[0..-2].join("#{separator} "), self[-1]].join(" #{type} ")
  end
end