Method: Faulty::Result#or_default

Defined in:
lib/faulty/result.rb

#or_default(default = nil) { ... } ⇒ Object

Get the ok value if this result is ok, otherwise return a default

Parameters:

  • default (defaults to: nil)

    The default value. Ignored if a block is given

Yields:

  • A block returning the default value

Returns:

  • The ok value or the default if this result is an error


128
129
130
131
132
133
134
135
136
# File 'lib/faulty/result.rb', line 128

def or_default(default = nil)
  if ok_unchecked?
    @ok
  elsif block_given?
    yield @error
  else
    default
  end
end