1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add pattern matching documentation

Add separate doc/syntax/pattern_matching.rdoc, add
link to control_expressions.rdoc.

The documentation is "reverse-engineered" from Ruby 2.7
behavior and early preview presentations, and corrected
by pattern-matching feature author @k-tsj.
This commit is contained in:
zverok 2019-12-25 20:39:42 +02:00 committed by Kazuki Tsujimoto
parent 8a7e0aaaef
commit 281b350058
Notes: git 2020-02-24 00:28:47 +09:00
3 changed files with 459 additions and 1 deletions

View file

@ -232,7 +232,7 @@ You may use +then+ after the +when+ condition. This is most frequently used
to place the body of the +when+ on a single line.
case a
when 1, 2 then puts "a is one or two
when 1, 2 then puts "a is one or two"
when 3 then puts "a is three"
else puts "I don't know what a is"
end
@ -255,6 +255,20 @@ Again, the +then+ and +else+ are optional.
The result value of a +case+ expression is the last value executed in the
expression.
Since Ruby 2.7, +case+ expressions also provide a more powerful experimental
pattern matching feature via the +in+ keyword:
case {a: 1, b: 2, c: 3}
in a: Integer => m
"matched: #{m}"
else
"not matched"
end
# => "matched: 1"
The pattern matching syntax is described on
{its own page}[rdoc-ref:syntax/pattern_matching.rdoc].
== +while+ Loop
The +while+ loop executes while a condition is true: