Class: Array

Inherits:
Object show all
Defined in:
lib/errgonomic/core_ext/array.rb,
lib/errgonomic/core_ext/blank.rb

Overview

Two additive lookups; no existing Array behavior changes, for the same reasons given in core_ext/hash.rb.

Instance Method Summary collapse

Instance Method Details

#fetch_option(index) ⇒ Object

Retrieve the element at an integer index, wrapped in an Option, following element presence as Rust's slice get does: an element holding nil is Some(nil); only an out-of-bounds index is None.

Examples:

a = [:a, nil]
a.fetch_option(0) # => Some(:a)
a.fetch_option(1) # => Some(nil)
a.fetch_option(2) # => None()


18
19
20
21
22
23
24
25
26
# File 'lib/errgonomic/core_ext/array.rb', line 18

def fetch_option(index)
  unless index.is_a?(::Integer)
    raise Errgonomic::TypeMismatchError,
          "index must be an Integer, got #{index.class}"
  end
  return None() unless (-length...length).cover?(index)

  Some(self[index])
end

#into_optionalObject

Wrap this array in an Errgonomic::OptionalArray view. The wrapper reads and writes this same array; use its to_a for a detached copy.

Examples:

a = [:a].into_optional
a[0] # => Some(:a)
a[1] # => None()


35
36
37
# File 'lib/errgonomic/core_ext/array.rb', line 35

def into_optional
  Errgonomic::OptionalArray.new(self)
end

#present?Boolean

:nodoc:

Returns:

  • (Boolean)


104
105
106
# File 'lib/errgonomic/core_ext/blank.rb', line 104

def present? # :nodoc:
  !empty?
end