Module: UnionType

Extended by:
Helper
Included in:
Either
Defined in:
lib/union_type.rb

Instance Method Summary collapse

Methods included from Helper

alias_names

Instance Method Details

#when(what) ⇒ Either

similar to Scala’s ‘match` for case class

will pattern match the value out and pass to matched lambda “‘ruby Right.new(1).when(->x{x+1 }) # => 2 Right.new(1).when(->x{x+1) # => nil Right.new(1) =~ (->x{x+1, _: ->xx-1 }) # => 0 “`

Returns:



13
14
15
16
17
18
19
20
# File 'lib/union_type.rb', line 13

def when(what)
  current_class = self.class.to_s.to_sym
  if what.include? current_class
    what[current_class].call(@v)
  elsif what.include? :_
    what[:_].call(@v)
  end
end