Issue #7986 has been updated by trans (Thomas Sawyer).


One does not necessarily have the luxury of putting the classes in order. Consider a case that uses inheritance:

  class Handler
    def handle(obj)
      case obj
      when Foo
        "Foo"
      end
    end
  end

  class SubHandler < Handler
    def handle(obj)
      result = super(obj)
      return result if result

      case obj
      when Bar
        p "Bar"
      end
    end 
  end

Now add a dozen or so additional classes and that is the use case I presently have.

As for the splat, one can do that for this particular example. (Though I wonder how efficient splatting in this way will be). I was just trying to give another example off the top of my head. But the point is that any method can be used, which is nice for it's flexibility.

    case.foo? name
    when a
      p "a foos name"
    when b
      p "b foos name, not a"
    else
      p "neither a nor b foos name"
    end

----------------------------------------
Feature #7986: Custom case statement comparison method
https://bugs.ruby-lang.org/issues/7986#change-37218

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: Next Major


=begin
Case statements use #=== to handle matching, but sometimes this can be limiting. It such cases it would be helpful to be able to specify the comparison method. So I wondered if that could be done by hanging the method off the `case` keyword, e.g.

    class Bar < Foo; end

    case == obj.class
    when Foo
      p "Foo, not Bar!"
    when Bar
      p "Bar!"
    end

And

    case.include? name
    when a
      p "a includes name"
    when b
      p "b includes name, not a"
    else
      p "neither a or b includes name"
    end
=end



-- 
http://bugs.ruby-lang.org/