Method: Functional::Tuple#fetch

Defined in:
lib/functional/tuple.rb

#fetch(index, default) ⇒ Object

Retrieve the item at the given index or return the given default value if the index is out of bounds. The behavior of indicies follows the rules for the ‘at` method.

Parameters:

  • index (Fixnum)

    the index of the item to be retrieved

  • default (Object)

    the value to return when given an out of bounds index

Returns:

  • (Object)

    the item at the given index or default when index is out of bounds

See Also:



68
69
70
71
72
73
74
# File 'lib/functional/tuple.rb', line 68

def fetch(index, default)
  if index >= length || -index > length
    default
  else
    at(index)
  end
end