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


@martin Being able to rely on inheritance and case statements I am able to remove hundreds of lines of complex DSL code. For example, old code:

    emit Foo, via: foo

    def emit_foo(obj)
       ...
    end

The new code:

    def emit(obj)
      case obj
      when Foo
        ...
      end
    end

While the first might look a bit prettier, the latter is far more flexible. This is a good example of the YADSL principle actually --don't create a DSL when regular code can do the job just as well or better. So that's the particular case I am presently looking at.

Regardless of my particular case though, doesn't being able to select the case operator seem like a nice bit of flexibility in itself? 

Another example that comes to mind:

    case.start_with? uri
    when "http:" then ...
    when "https:" then ...
    when "ftp:" then ...
    else ...
    end

Think of the efficiency improvements over the typical use of regular expressions in such a case.

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

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/