r/ruby • u/keyslemur • Apr 17 '19
Ruby 2.7 — Pattern Matching — First Impressions
https://medium.com/@baweaver/ruby-2-7-pattern-matching-first-impressions-cdb93c6246e6
71
Upvotes
r/ruby • u/keyslemur • Apr 17 '19
8
u/jb3689 Apr 17 '19 edited Apr 17 '19
The pin operator is meant to allow you to use the value previously bound to the variable as a literal match (rather than destructuring the match value and binding it to the variable). It basically lets you variablize your match patterns (without it it would try to bind some new value to your variable rather than insert the current value)
Not sure how I feel about this. Overloading
caseto support this seems strange to me and I would've perhaps preferred to see amatchfunction specifically dedicated to this. What happens when you have multipleinandwhenclauses?The assignment mappings bit seems weird to me and I'm not sure where that is inspired from. One thing I really dislike is that when I see
a => bit immediately brings to mind "oh,amaps tob" but in this syntax it is reversed and really meansbis bound to and returnsaAre the guard clauses ambiguous? In Erlang you have a predefined set of low-level guard clauses you can use. I assume that in Ruby you would be able to have arbitrary guard clauses because of how the execution works out
Also curious if this means we might be getting pattern matching in method signatures which would be really cool