[DOC] Update pattern matching docs for 3.2
* Remove section about experimental status * Add references to core objects that can deconstruct
This commit is contained in:
parent
4f049e915d
commit
ce0f3de032
Notes:
git
2025-04-09 13:49:37 +00:00
@ -415,6 +415,11 @@ Additionally, when matching custom classes, the expected class can be specified
|
|||||||
end
|
end
|
||||||
#=> "matched: 1"
|
#=> "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
|
== Guard clauses
|
||||||
|
|
||||||
+if+ can be used to attach an additional condition (guard clause) when the pattern matches. This condition may use bound variables:
|
+if+ can be used to attach an additional condition (guard clause) when the pattern matches. This condition may use bound variables:
|
||||||
@ -445,29 +450,6 @@ Additionally, when matching custom classes, the expected class can be specified
|
|||||||
end
|
end
|
||||||
#=> "matched"
|
#=> "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
|
== Appendix A. Pattern syntax
|
||||||
|
|
||||||
Approximate syntax is:
|
Approximate syntax is:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user