Class: MonadOxide::Either

Inherits:
Object
  • Object
show all
Defined in:
lib/either.rb

Direct Known Subclasses

Left, Right

Instance Method Summary collapse

Constructor Details

#initialize(_data) ⇒ Either

Returns a new instance of Either.

Raises:

  • (NoMethodError)


47
48
49
# File 'lib/either.rb', line 47

def initialize(_data)
  raise NoMethodError.new('Do not use Either directly. See Left and Right.')
end

Instance Method Details

#match(matcher) ⇒ R

Use pattern matching to work with both Left and Right variants. This is useful when it is desirable to have both variants handled in the same location. It can also be useful when either variant can coerced into a non-Result type.

Ruby has no built-in pattern matching, but the next best thing is a Hash using the Either classes themselves as the keys.

Tests for this are found in the tests of the Left and Right classes.

upon.

Parameters:

  • matcher (Hash<Class, Proc<T | E, R>] matcher The matcher to match)

    atcher [Hash<Class, Proc<T | E, R>] matcher The matcher to match

Options Hash (matcher):

  • MonadOxide::Left (Proc)

    The branch to execute for Left.

  • MonadOxide::Right (Proc)

    The branch to execute for Right.

Returns:

  • (R)

    The return value of the executed Proc.



67
68
69
# File 'lib/either.rb', line 67

def match(matcher)
  matcher[self.class()].call(@data)
end