Programming Ruby 3.2 (5th Edition): B1.0 page 165, : exception to the rule

@noelrappin

Page 165, paragraph before Case Pattern Matching, first line :

And second, you cannot do a variable assignment inside a pattern that uses the | to provide

That’s true, and the 3.1.2 documentation pattern_matching - RDoc Documentation also says (at the end of the section Variable binding) :

Variables that start with _ are the only exclusions from this rule:

case {a: 1, b: 2}
in {a: _, b: _foo} | Array
  "matched: #{_}, #{_foo}"
else
  "not matched"
end
# => "matched: 1, 2"

I left that out on purpose, because the _ is supposed to indicate a variable that you aren’t going to use, and frankly the whole behavior sounds weird. I’m open to revisiting that decision, but I kind of feel like there’s a non-zero chance that exception gets reversed at some point?

Thanks for all your great help with these issues!