Contents

Roo

Array.first!()

Used to return either the first or the first n elements of this array. The elements will be removed from the original array.

array.first!() as (Object or Nothing)
array.first!(n as Number) as Array

Parameters

  • n. Integer Number. The number of elements from this array to retrieve. Retrieval begins from index 0.

Returns

  • If this array is empty then the first form returns Nothing and the second form returns an empty Array object
  • If n > array.length then a new array with this array’s elements is returned but all elements are removed from the original array
  • Otherwise returns a new Array object containing the requested number of elements. Those elements are removed from this array

Example

var x = ["a", "b", "c", "d"]
var y = x.first!()
print(x) # => ["b", "c", "d"]
print(y) # => "a"

x = ["a", "b", "c", "d"]
y = x.first!(1) # =>["a"]
print(x) # => ["b", "c", "d"]
print(y) # => ["a"]

x = ["a", "b", "c", "d"]
y = x.first!(2)
print(x) # => ["c", "d"]
print(y) # => ["a", "b"]

x = ["a", "b", "c", "d"]
y = x.first!(3)
print(x) # => ["d"]
print(y) # => ["a", "b", "c"]

x = ["a", "b", "c", "d"]
y = x.first!(4)
print(x) # => []
print(y) # => ["a", "b", "c", "d"]

x = ["a", "b", "c", "d"]
y = x.first!(5)
print(x) # => []
print(y) # => ["a", "b", "c", "d"]