class NonEmptyArray

An enumerable which is guaranteed to not be empty.

Attributes

tail[R]

Public Class Methods

new(first_element, tail = []) click to toggle source
# File lib/non_empty_array.rb, line 14
def initialize(first_element, tail = [])
  @head = first_element
  @tail = tail
end

Public Instance Methods

all_but_last() click to toggle source
# File lib/non_empty_array.rb, line 32
def all_but_last
  T.must(all_elements.slice(0..-2))
end
each(&block) click to toggle source
# File lib/non_empty_array.rb, line 20
def each(&block)
  all_elements.each(&block)
end
last() click to toggle source
# File lib/non_empty_array.rb, line 25
def last
  return @head if @tail.empty?

  @tail[-1]
end
push(element) click to toggle source
# File lib/non_empty_array.rb, line 40
def push(element)
  @tail.push(element)
  self
end

Private Instance Methods

all_elements() click to toggle source
# File lib/non_empty_array.rb, line 48
def all_elements
  [@head] + @tail
end