Class: Hash

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

Overview

Two additive lookups; no existing Hash behavior changes. This is as deep as the gem reaches into Hash: the wider Ruby ecosystem leans on Hash semantics too heavily to patch them, so richer behavior lives in Errgonomic::OptionalHash instead.

Instance Method Summary collapse

Instance Method Details

#fetch_option(key) ⇒ Object

Retrieve the value for a key, wrapped in an Option, following key presence as Rust's HashMap#get does: a present key with a nil value is Some(nil); only a missing key is None.

Examples:

h = { color: :blue, shade: nil }
h.fetch_option(:color) # => Some(:blue)
h.fetch_option(:shade) # => Some(nil)
h.fetch_option(:smell) # => None()


20
21
22
23
24
# File 'lib/errgonomic/core_ext/hash.rb', line 20

def fetch_option(key)
  return None() unless key?(key)

  Some(self[key])
end

#into_optionalObject

Wrap this hash in an Errgonomic::OptionalHash view. The wrapper reads and writes this same hash; use its to_h for a detached copy.

Examples:

h = { color: :blue }.into_optional
h[:color] # => Some(:blue)
h[:smell] # => None()


33
34
35
# File 'lib/errgonomic/core_ext/hash.rb', line 33

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

#present?Boolean

:nodoc:

Returns:

  • (Boolean)


118
119
120
# File 'lib/errgonomic/core_ext/blank.rb', line 118

def present? # :nodoc:
  !empty?
end