Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/coolkit/array/remove_first.rb

Instance Method Summary collapse

Instance Method Details

#remove_first(item) ⇒ self

Return a copy of an array the first instance of a given item removed.

Parameters:

  • item (Object)

Returns:

  • (self)


18
19
20
# File 'lib/coolkit/array/remove_first.rb', line 18

def remove_first(item)
  return self.dup.tap { |s| s.remove_first!(item) }
end

#remove_first!(item) ⇒ self

Remove the first instance of an item from an array. Note: this will mutate the reciever.

Parameters:

  • item (Object)

Returns:

  • (self)


7
8
9
10
11
12
# File 'lib/coolkit/array/remove_first.rb', line 7

def remove_first!(item)
  return self if self.length == 0
  index = self.index(item)
  self.delete_at(index) if index
  return self
end