https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-03-01T12:56:16ZRuby Issue Tracking SystemRuby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372152013-03-01T12:56:16Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>=begin<br>
Why not use the features of case statements properly? For classes, place the subclass above the superclass. For array inclusion, use splat:</p>
<p>class Foo; end<br>
class Bar < Foo; end</p>
<p>def case_klass obj<br>
case obj<br>
when Bar<br>
p "Bar!"<br>
when Foo<br>
p "Foo, not Bar!"<br>
end<br>
end</p>
<p>case_klass Foo.new<br>
case_klass Bar.new</p>
<p>def case_include obj<br>
case obj<br>
when *%w[b]<br>
p "b includes name, not a"<br>
when *%w[a]<br>
p "a includes name"<br>
else<br>
p "neither a or b includes name"<br>
end<br>
end</p>
<p>case_include 'a'<br>
case_include 'b'<br>
case_include 'c'</p>
<p>=end</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372182013-03-01T17:42:37Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
One does not necessarily have the luxury of putting the classes in order. Consider a case that uses inheritance:</p>
<p>class Handler<br>
def handle(obj)<br>
case obj<br>
when Foo<br>
"Foo"<br>
end<br>
end<br>
end</p>
<p>class SubHandler < Handler<br>
def handle(obj)<br>
result = super(obj)<br>
return result if result</p>
<pre><code> case obj
when Bar
"Bar"
end
end
</code></pre>
<p>end</p>
<p>Now add a dozen or so additional classes and that is basically the use case I presently have.</p>
<p>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.</p>
<pre><code>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
</code></pre>
<p>=end</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372192013-03-01T18:53:12Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>On 2013/03/01 17:42, trans (Thomas Sawyer) wrote:</p>
<blockquote>
<p>One does not necessarily have the luxury of putting the classes in order. Consider a case that uses inheritance:</p>
</blockquote>
<p>[snip]</p>
<blockquote>
<p>Now add a dozen or so additional classes and that is the use case I presently have.</p>
</blockquote>
<p>I'm of course not familiar with your problem or program, but a<br>
proliferation of case statements, or case alternatives, is usually taken<br>
as a sign that there is something more fundamental that needs to be<br>
improved.</p>
<p>Regards, Martin.</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372232013-03-01T22:45:34Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
@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:</p>
<pre><code>emit Foo, via: foo
def emit_foo(obj)
...
end
</code></pre>
<p>The new code:</p>
<pre><code>def emit(obj)
case obj
when Foo
...
end
end
</code></pre>
<p>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.</p>
<p>Regardless of my particular case though, doesn't being able to select the case operator seem like a nice bit of flexibility in itself?</p>
<p>Another example that comes to mind:</p>
<pre><code>case.start_with? uri
when "https:"
...
when "http:"
...
when "ftp:"
...
else
...
end
</code></pre>
<p>Think of the efficiency improvements over the typical use of regular expressions in such a case.<br>
=end</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372432013-03-02T05:43:43Zdrbrain (Eric Hodel)drbrain@segment7.net
<ul></ul><p>=begin<br>
It is puzzling that you must always super first, then if unhandled execute the case expression, but do nothing if the object is unknown. It seems very error prone.</p>
<p>Why not execute the subclass case expression first, then super in its else?</p>
<p>As such, I agree with Martin that there is something wrong with your design. If you study design patterns you'll find that dispatching to methods is preferred (such as in the visitor pattern) to using comparison constructs due to the inflexibility that your design displays.</p>
<p>A case expression can already handle URI matching just fine:</p>
<p>case uri<br>
when /\Ahttps:/i then …<br>
when /\Ahttp:/i then …<br>
when /\Aftp:/i then …<br>
else …<br>
end</p>
<p>or if you use URI objects:</p>
<p>case uri<br>
when URI::HTTPS then …<br>
when URI::HTTP then …<br>
when URI::FTP then …<br>
else …<br>
end</p>
<p>or:</p>
<p>case uri.scheme.downcase<br>
when 'https' then …<br>
when 'http' then …<br>
when 'ftp' then …<br>
else …<br>
end</p>
<p>=end</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=372442013-03-02T05:59:09Ztrans (Thomas Sawyer)
<ul></ul><p>=begin</p>
<blockquote>
<p>"It is puzzling that you must always super first, then if unhandled execute the case expression, but do nothing if the object is unknown. It seems very error prone."</p>
</blockquote>
<p>Why? That's not uncommon. In this particular case you don't "always". It depends on which you want to take precedence, the superclass or the subclass. Recall my case is in place of a DSL --it's up to the end developer to decide which. Super may even be called in the middle somewhere. It is not at all error prone in the least.</p>
<blockquote>
<p>A case expression can already handle URI matching just fine</p>
</blockquote>
<p>Of course, and I mentioned that. And what did I say? Quote: "think of the efficiency improvements over the typical use of regular expressions". Moreover the point has nothing whatsoever to do with URIs.</p>
<p>I could sit here and give 100 different examples and for everyone, someone could say, well you could also do it this way. In the end you can replace every case statement with if/else too.<br>
=end</p> Ruby master - Feature #7986: Custom case statement comparison methodhttps://redmine.ruby-lang.org/issues/7986?journal_id=890962020-12-10T08:50:14Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li><li><strong>Target version</strong> deleted (<del><i>3.0</i></del>)</li></ul>