Skip to content

Latest commit

 

History

History
37 lines (30 loc) · 617 Bytes

array_second.md

File metadata and controls

37 lines (30 loc) · 617 Bytes

Description

Define a new instance method on the Array class called second, which returns the second item in an array (similar to the way .first and .last work in Ruby).

If there is no element with index 1 in the array, return nil.

class Array

end

Examples

[3, 4, 5].second  #  => 4 
[].second         #  => nil

My Solution

class Array
  def second
    self[1]
  end
end

Better/Alternative solution from Codewars

class Array
  def second
    self.at(1)
  end
end