Method: Object#present_or

Defined in:
lib/errgonomic/presence.rb

#present_or(value) ⇒ Object

Returns the receiver if it is present, otherwise returns the given value. If constructing the default value is expensive, consider using present_or_else.

Parameters:

  • value (Object)

    The value to return if the receiver is not present.

Returns:

  • (Object)

    The receiver if it is present, otherwise the given value.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/errgonomic/presence.rb', line 29

def present_or(value)
  # TBD whether this is *too* strict
  if value.class != self.class && self.class != NilClass
    raise Errgonomic::TypeMismatchError,
          "Type mismatch: default value is a #{value.class} but original was a #{self.class}"
  end

  return self if present?

  value
end