Module: Either

Defined in:
lib/either.rb

Defined Under Namespace

Classes: Left, Right

Class Method Summary collapse

Class Method Details

.match(e, left:, right:) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/either.rb', line 17

def self.match(e, left:, right:)
  if e.right?
    right.call(e.send(:val))
  elsif e.left?
    left.call(e.send(:val))
  end
end

.new(&block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/either.rb', line 4

def self.new(&block)
  val = begin
          block.call()
        rescue Exception => e
          e
        end
  if val.nil? || val.is_a?(Exception)
    Left.new(val)
  else
    Right.new(val)
  end
end