Class: Array

Inherits:
Object show all
Defined in:
lib/volt/extra_core/array.rb,
lib/volt/extra_core/blank.rb

Instance Method Summary collapse

Instance Method Details

#sumObject



2
3
4
# File 'lib/volt/extra_core/array.rb', line 2

def sum
  inject(0, :+)
end

#to_hObject

For some reason .to_h doesn’t show as defined in opal, but defined?(:to_h) returns true.



8
9
10
# File 'lib/volt/extra_core/array.rb', line 8

def to_h
  Hash[self]
end

#to_sentence(options = {}) ⇒ Object

Converts an array to a sentence



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/volt/extra_core/array.rb', line 14

def to_sentence(options={})
  conjunction = options.fetch(:conjunction, 'and')
  comma       = options.fetch(:comma, ',')
  oxford      = options.fetch(:oxford, true) # <- true is the right value

  case size
  when 0
    ''
  when 1
    self[0].to_s
  when 2
    self.join(" #{conjunction} ")
  else
    str = self[0..-2].join(comma + ' ')
    str += comma if oxford
    str += " #{conjunction} " + self[-1].to_s
    str
  end
end