diff options
author | Kazuki Tsujimoto <[email protected]> | 2020-12-13 11:50:14 +0900 |
---|---|---|
committer | Kazuki Tsujimoto <[email protected]> | 2020-12-13 11:51:49 +0900 |
commit | 88f3ce12d32ffbef983b0950743c20253ea2d0c6 (patch) | |
tree | 8ff07aa837af75a4e389ef30d139fb391be63e0f /doc/syntax/pattern_matching.rdoc | |
parent | a8cf526ae99c9e0823cd8c7c5ac96e43061fafa6 (diff) |
Reintroduce `expr in pat` [Feature #17371]
Diffstat (limited to 'doc/syntax/pattern_matching.rdoc')
-rw-r--r-- | doc/syntax/pattern_matching.rdoc | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/doc/syntax/pattern_matching.rdoc b/doc/syntax/pattern_matching.rdoc index 8419af58bb..b4ac52e0a9 100644 --- a/doc/syntax/pattern_matching.rdoc +++ b/doc/syntax/pattern_matching.rdoc @@ -15,11 +15,13 @@ Pattern matching in Ruby is implemented with the +case+/+in+ expression: ... end -or with the +=>+ operator, which can be used in a standalone expression: +(Note that +in+ and +when+ branches can *not* be mixed in one +case+ expression.) + +or with the +=>+ operator and the +in+ operator, which can be used in a standalone expression: <expression> => <pattern> -(Note that +in+ and +when+ branches can *not* be mixed in one +case+ expression.) + <expression> in <pattern> Pattern matching is _exhaustive_: if variable doesn't match pattern (in a separate +in+ clause), or doesn't matches any branch of +case+ expression (and +else+ branch is absent), +NoMatchingPatternError+ is raised. @@ -46,6 +48,12 @@ whilst the +=>+ operator is most useful when expected data structure is known be puts "Connect with user '#{user}'" # Prints: "Connect with user 'admin'" ++<expression> in <pattern>+ is the same as +case <expression>; in <pattern>; true; else false; end+. +You can use it when you only want to know if a pattern has been matched or not: + + users = [{name: "Alice", age: 12}, {name: "Bob", age: 23}] + users.any? {|u| u in {name: /B/, age: 20..} } #=> true + See below for more examples and explanations of the syntax. == Patterns |