Feature #14913


Extend case to match several values at once

Added by zverok (Victor Shepelev) about 4 years ago. Updated about 4 years ago.

Target version:


This proposal is part of the search for how pattern matching or its elements could be gently introduced to Ruby.
This blog post (authored by me) provides context and "full" (more powerful than the current proposal, but more questionable also) idea, but I believe that this, more moderate, extension, could be a reasonable start.


Allow matching several values at once, when matching by case.

case (x, y)
when (0..10, 0..10)
when (Array, nil) # coordinates were passed as an array in first argument
when (Numeric, nil), (nil, Numeric) # somehow one of coordinates were missing


  • The syntax change is minimal (no new keywords/special chars), yet visible (very little possibility of hidden incompatibilities)
  • It is alike deconstruction when passing arguments to methods or blocks
  • It allows gradual adding of more features in the future versions of Ruby once the new syntax will become familiar:
case (x, *y) # flatten y
when (*Numeric) # match only an array of numerics
when (_, Array) # skip any
when (Numeric, Numeric => y, Hash => options) # match & assign, like in rescue


  • Blog post with reasons, links to previous discussions and more examples
  • Experimental gem to try as much of new proposals as possible to imitate in the current Ruby version.


Alternative syntax (reusing "block arguments" sign):

case |x, y|
when |0..10, 0..10|
when |Array, nil|
when |Numeric, nil|, |nil, Numeric| # OK, that's probably weird

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #14916: Proposal to add Array#===OpenActions

Updated by shevegen (Robert A. Heiler) about 4 years ago

I don't have a big pro or con opinion on the functionality itself.

However had, for the alternative syntax of:

case |x, y|

I think it is too reminiscent of block syntax style such as:

object.each {|x, y|

I would rather prefer to keep it simpler and not have this syntax
for case/when structures.

Updated by sawa (Tsuyoshi Sawada) about 4 years ago

Perhaps, you can redefine Array#=== and use arrays in the following way:

case [x, y]
when [0..10, 0..10]
when [Array, nil]
when [Numeric, nil], [nil, Numeric]
Actions #3

Updated by mrkn (Kenta Murata) about 4 years ago

Actions #4

Updated by shyouhei (Shyouhei Urabe) about 4 years ago

  • Related to deleted (Feature #14912: Introduce pattern matching syntax)

Updated by matz (Yukihiro Matsumoto) about 4 years ago

  • Status changed from Open to Closed

We will discuss the pattern matching in #14912.


Actions #6

Updated by mrkn (Kenta Murata) about 4 years ago


Also available in: Atom PDF