lib/indentation/array_mod.rb in indentation-0.0.7 vs lib/indentation/array_mod.rb in indentation-0.1.1
- old
+ new
@@ -69,6 +69,27 @@
# Can pass an optional modifier that changes the indentation amount removed
def reset_indentation!(modifier = 0)
indent!(-find_least_indentation + modifier)
end
+ # Join an array of strings using English list punctuation.
+ def english_join(conjunction = 'and', separator = ', ', oxford_comma = true)
+ len = self.length
+ return '' if len == 0
+ return self[0].to_s if len == 1
+ return "#{self[0].to_s} #{conjunction} #{self[1].to_s}" if len == 2
+ join_str = ''
+ self.each_with_index{|ele, i|
+ str = if !oxford_comma && i == len - 2
+ "#{ele} #{conjunction} "
+ elsif i == len - 2
+ "#{ele}#{separator}#{conjunction} "
+ elsif i == len - 1
+ "#{ele}"
+ else
+ "#{ele}#{separator}"
+ end
+ join_str << str
+ }
+ join_str
+ end
end
\ No newline at end of file