Method: Array#init

Defined in:
lib/fr/array.rb

#init(n = 1) ⇒ Array

Selects all elements except the last ‘n` ones.

Examples:

[1, 2, 3].init      #=> [1, 2]
[1, 2, 3].init(2)   #=> [1]
[].tail             #=> []

Returns:

Raises:

  • (ArgumentError)


48
49
50
51
# File 'lib/fr/array.rb', line 48

def init(n = 1)
  raise ArgumentError, "n cannot be negative" if n < 0
  slice(0..-(n + 1)) or []
end