Class: Array

Inherits:
Object show all
Defined in:
lib/jun/active_support/core_ext/array/access.rb,
lib/jun/active_support/core_ext/array/conversion.rb

Instance Method Summary collapse

Instance Method Details

#fifthObject

Returns the fifth element in the array.



20
21
22
# File 'lib/jun/active_support/core_ext/array/access.rb', line 20

def fifth
  self[4]
end

#fourthObject

Returns the fourth element in the array.



15
16
17
# File 'lib/jun/active_support/core_ext/array/access.rb', line 15

def fourth
  self[3]
end

#secondObject

Returns the second element in the array.



5
6
7
# File 'lib/jun/active_support/core_ext/array/access.rb', line 5

def second
  self[1]
end

#second_to_lastObject

Returns the second-to-last element in the array.



30
31
32
# File 'lib/jun/active_support/core_ext/array/access.rb', line 30

def second_to_last
  self[-2]
end

#thirdObject

Returns the third element in the array.



10
11
12
# File 'lib/jun/active_support/core_ext/array/access.rb', line 10

def third
  self[2]
end

#third_to_lastObject

Returns the third-to-last element in the array.



25
26
27
# File 'lib/jun/active_support/core_ext/array/access.rb', line 25

def third_to_last
  self[-3]
end

#to_sentence(delimiter: ", ", last_delimiter: "and") ⇒ Object

Converts the array to a comma-separated sentence.

["one", "two", "three"].to_sentence                 #=> "one, two and three"
["left", "right"].to_sentence(last_delimiter: "or") #=> "left or right"
[].to_sentence                                      #=> ""

Parameters:

  • delimiter (String) (defaults to: ", ")

    the delimiter value for connecting array elements.

  • last_delimiter (String) (defaults to: "and")

    the connecting word for the last array element.



12
13
14
15
16
17
# File 'lib/jun/active_support/core_ext/array/conversion.rb', line 12

def to_sentence(delimiter: ", ", last_delimiter: "and")
  return "" if none?
  return self.first if one?

  "#{self[0...-1].join(delimiter)} #{last_delimiter} #{self[-1]}"
end