Module: Rstructural::Either

Extended by:
ADT
Defined in:
lib/rstructural/either.rb

Constant Summary collapse

Left =
data :value
Right =
data :value

Class Method Summary collapse

Methods included from ADT

const, data, extended, interface

Class Method Details

.left(obj) ⇒ Object



10
11
12
# File 'lib/rstructural/either.rb', line 10

def self.left(obj)
  Left.new(obj)
end

.right(obj) ⇒ Object



14
15
16
# File 'lib/rstructural/either.rb', line 14

def self.right(obj)
  Right.new(obj)
end

.try(catch_nil: false, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/rstructural/either.rb', line 18

def self.try(catch_nil: false, &block)
  result = block.call
  if catch_nil && result.nil?
    Left.new(nil)
  else
    Right.new(result)
  end
rescue => e
  Left.new(e)
end