diff options
Diffstat (limited to 'doc/syntax/pattern_matching.rdoc')
-rw-r--r-- | doc/syntax/pattern_matching.rdoc | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/doc/syntax/pattern_matching.rdoc b/doc/syntax/pattern_matching.rdoc index f80703d5c6..e49c09a1f8 100644 --- a/doc/syntax/pattern_matching.rdoc +++ b/doc/syntax/pattern_matching.rdoc @@ -140,7 +140,7 @@ Both array and hash patterns support "rest" specification: end #=> "matched" -In +case+ (but not in <code>=></code> and +in+) expressions, parentheses around both kinds of patterns could be omitted: +Parentheses around both kinds of patterns could be omitted: case [1, 2] in Integer, Integer @@ -158,6 +158,12 @@ In +case+ (but not in <code>=></code> and +in+) expressions, parentheses around end #=> "matched" + [1, 2] => a, b + [1, 2] in a, b + + {a: 1, b: 2, c: 3} => a: + {a: 1, b: 2, c: 3} in a: + Find pattern is similar to array pattern but it can be used to check if the given object has any elements that match the pattern: case ["a", 1, "b", "c", 2] @@ -409,6 +415,11 @@ Additionally, when matching custom classes, the expected class can be specified end #=> "matched: 1" +These core and library classes implement deconstruction: + +* MatchData#deconstruct and MatchData#deconstruct_keys; +* Time#deconstruct_keys, Date#deconstruct_keys, DateTime#deconstruct_keys. + == Guard clauses +if+ can be used to attach an additional condition (guard clause) when the pattern matches. This condition may use bound variables: @@ -439,29 +450,6 @@ Additionally, when matching custom classes, the expected class can be specified end #=> "matched" -== Current feature status - -As of Ruby 3.1, find patterns are considered _experimental_: its syntax can change in the future. Every time you use these features in code, a warning will be printed: - - [0] => [*, 0, *] - # warning: Find pattern is experimental, and the behavior may change in future versions of Ruby! - # warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby! - -To suppress this warning, one may use the Warning::[]= method: - - Warning[:experimental] = false - eval('[0] => [*, 0, *]') - # ...no warning printed... - -Note that pattern-matching warnings are raised at compile time, so this will not suppress the warning: - - Warning[:experimental] = false # At the time this line is evaluated, the parsing happened and warning emitted - [0] => [*, 0, *] - -So, only subsequently loaded files or `eval`-ed code is affected by switching the flag. - -Alternatively, the command line option <code>-W:no-experimental</code> can be used to turn off "experimental" feature warnings. - == Appendix A. Pattern syntax Approximate syntax is: |