Sha256: 546a7e32e05e617c854d1f1a261079e0df055e4fe21a3cead19879f3ba26330f
Contents?: true
Size: 1.53 KB
Versions: 3
Compression:
Stored size: 1.53 KB
Contents
--- title: Pattern matching layout: gem-single name: dry-monads --- Ruby 2.7 introduces pattern matchings, it is nicely supported by dry-monads 1.3+. ### Matching Result values ```ruby # presumably you do it in a class with `include Dry::Monads[:result]` case value in Success(Integer => x) # x is bound to an integer in Success[:created, user] # alternatively: Success([:created, user]) # user is bound to the second member in Success(Date | Time) # date or time object in Success([1, *]) # any array starting with 1 in Success(String => s) if s.size < 100 # only if `s` is short enough in Success({ counter: Integer }) # matches Success(counter: 50) # doesn't match Success(counter: 50, extra: 50) in Success({ user: User, account: Account => user_account, ** }) # matches Success(user: User.new(...), account: Account.new(...), else: ...) # user_account is bound to the value of the `:account` key in Success() # corresponds to Success(Unit) in Success(_) # general success in Failure[:user_not_found] # Failure([:user_not_found]) in Failure[error_code, *payload] # ... end ``` In the sippet above, the patterns will be tried sequentially. If `value` doesn't match any pattern, an error will be thrown. ### Matching Maybe ```ruby case value in Some(Integer => x) # x is an integer in Some(Float | String) # ... in None # ... end ``` ### Matching List ```ruby case value in List[Integer] # any list of size 1 with an integer in List[1, 2, 3, *] # list with size >= 3 starting with 1, 2, 3 in List[] # empty list end ```
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
dry-monads-1.3.4 | docsite/source/pattern-matching.html.md |
dry-monads-1.3.3 | docsite/source/pattern-matching.html.md |
dry-monads-1.3.2 | docsite/source/pattern-matching.html.md |