模式匹配¶ ↑
模式匹配是一种允许对结构化值进行深度匹配的功能:检查结构并将匹配的部分绑定到局部变量。
Ruby 中的模式匹配使用 case
/in
表达式实现
case <expression> in <pattern1> ... in <pattern2> ... in <pattern3> ... else ... end
(请注意,in
和 when
分支不能在一个 case
表达式中混合使用。)
或者使用 =>
运算符和 in
运算符,它们可以在独立表达式中使用
<expression> => <pattern> <expression> in <pattern>
case
/in
表达式是穷举的:如果表达式的值与 case
表达式的任何分支都不匹配(并且 else
分支不存在),则会引发 NoMatchingPatternError
。
因此,case
表达式可用于条件匹配和解包
config = {db: {user: 'admin', password: 'abc123'}} case config in db: {user:} # matches subhash and puts matched value in variable user puts "Connect with user '#{user}'" in connection: {username: } puts "Connect with user '#{username}'" else puts "Unrecognized structure of config" end # Prints: "Connect with user 'admin'"
而 =>
运算符在预先知道预期数据结构时最有用,只需解包其中的一部分
config = {db: {user: 'admin', password: 'abc123'}} config => {db: {user:}} # will raise if the config's structure is unexpected puts "Connect with user '#{user}'" # Prints: "Connect with user 'admin'"
<expression> in <pattern>
等同于 case <expression>; in <pattern>; true; else false; end
。当您只想了解模式是否匹配时,可以使用它
users = [{name: "Alice", age: 12}, {name: "Bob", age: 23}] users.any? {|user| user in {name: /B/, age: 20..} } #=> true
有关更多示例和语法说明,请参见下文。
模式¶ ↑
模式可以是
-
任何 Ruby 对象(由
===
运算符匹配,如when
中);(值模式) -
数组模式:
[<subpattern>, <subpattern>, <subpattern>, ...]
;(数组模式) -
查找模式:
[*variable, <subpattern>, <subpattern>, <subpattern>, ..., *variable]
;(查找模式) -
哈希模式:
{key: <subpattern>, key: <subpattern>, ...}
;(哈希模式) -
使用
|
组合模式;(备选模式) -
变量捕获:
<pattern> => variable
或variable
;(作为模式,变量模式)
任何模式都可以嵌套在数组/查找/哈希模式中,其中指定了 <subpattern>
。
Array
模式和查找模式匹配数组,或响应 deconstruct
的对象(有关后者的信息,请参见下文)。Hash
模式匹配哈希,或响应 deconstruct_keys
的对象(有关后者的信息,请参见下文)。请注意,哈希模式仅支持符号键。
数组和哈希模式行为之间的一个重要区别是,数组仅匹配整个数组
case [1, 2, 3] in [Integer, Integer] "matched" else "not matched" end #=> "not matched"
而哈希即使存在除指定部分以外的其他键也会匹配
case {a: 1, b: 2, c: 3} in {a: Integer} "matched" else "not matched" end #=> "matched"
{}
是此规则的唯一例外。它仅在给出空哈希时匹配
case {a: 1, b: 2, c: 3} in {} "matched" else "not matched" end #=> "not matched" case {} in {} "matched" else "not matched" end #=> "matched"
还有一种方法可以指定匹配的哈希中不应存在除模式明确指定的键以外的其他键,使用 **nil
case {a: 1, b: 2} in {a: Integer, **nil} # this will not match the pattern having keys other than a: "matched a part" in {a: Integer, b: Integer, **nil} "matched a whole" else "not matched" end #=> "matched a whole"
数组和哈希模式都支持“剩余”规范
case [1, 2, 3] in [Integer, *] "matched" else "not matched" end #=> "matched" case {a: 1, b: 2, c: 3} in {a: Integer, **} "matched" else "not matched" end #=> "matched"
两种模式周围的括号可以省略
case [1, 2] in Integer, Integer "matched" else "not matched" end #=> "matched" case {a: 1, b: 2, c: 3} in a: Integer "matched" else "not matched" 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:
查找模式类似于数组模式,但它可以用来检查给定对象是否包含任何与模式匹配的元素。
case ["a", 1, "b", "c", 2] in [*, String, String, *] "matched" else "not matched" end
变量绑定¶ ↑
除了深度结构检查之外,模式匹配的一个非常重要的功能是将匹配的部分绑定到局部变量。绑定的基本形式是在匹配的(子)模式之后简单地指定=> variable_name
(人们可能会发现这类似于在rescue ExceptionClass => var
子句中将异常存储在局部变量中)。
case [1, 2] in Integer => a, Integer "matched: #{a}" else "not matched" end #=> "matched: 1" case {a: 1, b: 2, c: 3} in a: Integer => m "matched: #{m}" else "not matched" end #=> "matched: 1"
如果不需要额外的检查,只是将数据的一部分绑定到变量,可以使用更简单的形式。
case [1, 2] in a, Integer "matched: #{a}" else "not matched" end #=> "matched: 1" case {a: 1, b: 2, c: 3} in a: m "matched: #{m}" else "not matched" end #=> "matched: 1"
对于哈希模式,甚至存在更简单的形式:仅键规范(不带任何子模式)也会将局部变量绑定到键的名称。
case {a: 1, b: 2, c: 3} in a: "matched: #{a}" else "not matched" end #=> "matched: 1"
Binding
也适用于嵌套模式。
case {name: 'John', friends: [{name: 'Jane'}, {name: 'Rajesh'}]} in name:, friends: [{name: first_friend}, *] "matched: #{first_friend}" else "not matched" end #=> "matched: Jane"
模式的“剩余”部分也可以绑定到变量。
case [1, 2, 3] in a, *rest "matched: #{a}, #{rest}" else "not matched" end #=> "matched: 1, [2, 3]" case {a: 1, b: 2, c: 3} in a:, **rest "matched: #{a}, #{rest}" else "not matched" end #=> "matched: 1, {:b=>2, :c=>3}"
Binding
目前不适用于使用|
连接的备选模式。
case {a: 1, b: 2} in {a: } | Array "matched: #{a}" else "not matched" end # SyntaxError (illegal variable in alternative pattern (a))
以_
开头的变量是此规则的唯一例外。
case {a: 1, b: 2} in {a: _, b: _foo} | Array "matched: #{_}, #{_foo}" else "not matched" end # => "matched: 1, 2"
但是,不建议重用绑定值,因为这种模式的目标是表示一个被丢弃的值。
变量固定¶ ↑
由于变量绑定的功能,现有的局部变量不能直接用作子模式。
expectation = 18 case [1, 2] in expectation, *rest "matched. expectation was: #{expectation}" else "not matched. expectation was: #{expectation}" end # expected: "not matched. expectation was: 18" # real: "matched. expectation was: 1" -- local variable just rewritten
在这种情况下,可以使用固定运算符^
,告诉 Ruby“只将此值用作模式的一部分”。
expectation = 18 case [1, 2] in ^expectation, *rest "matched. expectation was: #{expectation}" else "not matched. expectation was: #{expectation}" end #=> "not matched. expectation was: 18"
变量固定的一个重要用法是指定相同的值应该在模式中出现多次。
jane = {school: 'high', schools: [{id: 1, level: 'middle'}, {id: 2, level: 'high'}]} john = {school: 'high', schools: [{id: 1, level: 'middle'}]} case jane in school:, schools: [*, {id:, level: ^school}] # select the last school, level should match "matched. school: #{id}" else "not matched" end #=> "matched. school: 2" case john # the specified school level is "high", but last school does not match in school:, schools: [*, {id:, level: ^school}] "matched. school: #{id}" else "not matched" end #=> "not matched"
除了固定局部变量之外,还可以固定实例、全局和类变量。
$gvar = 1 class A @ivar = 2 @@cvar = 3 case [1, 2, 3] in ^$gvar, ^@ivar, ^@@cvar "matched" else "not matched" end #=> "matched" end
您还可以使用括号固定任意表达式的结果。
a = 1 b = 2 case 3 in ^(a + b) "matched" else "not matched" end #=> "matched"
匹配非基本对象:deconstruct
和 deconstruct_keys
¶ ↑
如上所述,除了字面数组和哈希之外,数组、查找和哈希模式将尝试匹配任何实现deconstruct
(对于数组/查找模式)或deconstruct_keys
(对于哈希模式)的对象。
class Point def initialize(x, y) @x, @y = x, y end def deconstruct puts "deconstruct called" [@x, @y] end def deconstruct_keys(keys) puts "deconstruct_keys called with #{keys.inspect}" {x: @x, y: @y} end end case Point.new(1, -2) in px, Integer # sub-patterns and variable binding works "matched: #{px}" else "not matched" end # prints "deconstruct called" "matched: 1" case Point.new(1, -2) in x: 0.. => px "matched: #{px}" else "not matched" end # prints: deconstruct_keys called with [:x] #=> "matched: 1"
keys
传递给 deconstruct_keys
以在匹配的类中提供优化空间:如果计算完整的哈希表示很昂贵,则可以仅计算必要的子哈希。当使用 **rest
模式时,nil
将作为 keys
值传递。
case Point.new(1, -2) in x: 0.. => px, **rest "matched: #{px}" else "not matched" end # prints: deconstruct_keys called with nil #=> "matched: 1"
此外,在匹配自定义类时,可以将预期类指定为模式的一部分,并使用 ===
进行检查。
class SuperPoint < Point end case Point.new(1, -2) in SuperPoint(x: 0.. => px) "matched: #{px}" else "not matched" end #=> "not matched" case SuperPoint.new(1, -2) in SuperPoint[x: 0.. => px] # [] or () parentheses are allowed "matched: #{px}" else "not matched" end #=> "matched: 1"
这些核心和库类实现了解构
-
Time#deconstruct_keys
、Date#deconstruct_keys、DateTime#deconstruct_keys。
保护子句¶ ↑
if
可用于在模式匹配时附加一个额外的条件(保护子句)。此条件可以使用绑定变量
case [1, 2] in a, b if b == a*2 "matched" else "not matched" end #=> "matched" case [1, 1] in a, b if b == a*2 "matched" else "not matched" end #=> "not matched"
unless
也适用
case [1, 1] in a, b unless b == a*2 "matched" else "not matched" end #=> "matched"
附录 A. 模式语法¶ ↑
近似语法是
pattern: value_pattern | variable_pattern | alternative_pattern | as_pattern | array_pattern | find_pattern | hash_pattern value_pattern: literal | Constant | ^local_variable | ^instance_variable | ^class_variable | ^global_variable | ^(expression) variable_pattern: variable alternative_pattern: pattern | pattern | ... as_pattern: pattern => variable array_pattern: [pattern, ..., *variable] | Constant(pattern, ..., *variable) | Constant[pattern, ..., *variable] find_pattern: [*variable, pattern, ..., *variable] | Constant(*variable, pattern, ..., *variable) | Constant[*variable, pattern, ..., *variable] hash_pattern: {key: pattern, key:, ..., **variable} | Constant(key: pattern, key:, ..., **variable) | Constant[key: pattern, key:, ..., **variable]
附录 B. 一些未定义行为示例¶ ↑
为了在未来留下优化空间,规范包含一些未定义的行为。
在未匹配的模式中使用变量
case [0, 1] in [a, 2] "not matched" in b "matched" in c "not matched" end a #=> undefined c #=> undefined
deconstruct
、deconstruct_keys
方法调用的次数
$i = 0 ary = [0] def ary.deconstruct $i += 1 self end case ary in [0, 1] "not matched" in [0] "matched" end $i #=> undefined