Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/asciidoc-bib/extensions.rb
Overview
Two methods are added to extend the Array class.
Instance Method Summary collapse
-
#comma_and_join ⇒ Object
Join items in array using commas and ‘and’ on last item.
-
#third ⇒ Object
Retrieve the third item of an array Note: no checks for validity.
Instance Method Details
#comma_and_join ⇒ Object
Join items in array using commas and ‘and’ on last item
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/asciidoc-bib/extensions.rb', line 12 def comma_and_join if size < 2 return self.join("") end result = "".dup self.each_with_index do |item, index| if index.zero? result << item elsif index == size-1 result << " and #{item}" else result << ", #{item}" end end return result end |
#third ⇒ Object
Retrieve the third item of an array Note: no checks for validity
7 8 9 |
# File 'lib/asciidoc-bib/extensions.rb', line 7 def third self[2] end |