Module: UnionType
Instance Method Summary collapse
-
#when(what) ⇒ Either
similar to Scala’s ‘match` for case class.
Methods included from Helper
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 “`
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 |