Duplicated when clause is ignored

I recieve quite some errors like this.

foobar.rb:156: warning: duplicated when clause is ignored

I understand that there are multiple when clauses.

Is there any way for Ruby to tell me which are duplicated though?

If I would know which ones are duplicated, I could remove those entries
and make ruby happy.

On 7/16/11 11:50 PM, Marc H. wrote:

foobar.rb:156: warning: duplicated when clause is ignored

Is there any way for Ruby to tell me which are duplicated though?

Not that I know of, rb_compile_warning() is invoked from compile.c with
the line number on which the ‘case’ statement is.
I agree that it would be a nice feature.

The problem, though, seems to be elsewhere. How many ‘when’ clauses are
there in your script? Is it so high to make a manual check too
difficult?
If so, that might be a code smell, meaning that you should refactor your
code.

From a couple of tests I made, I observed something interesting.

This:

x = 1
case x
when 0
when 1
when 1
end

will cause the warning to appear.

This, on the other hand:

x = 1
case x
when 0
when 1
when 1 + 0
when 1
end

is enough to fool iseq_set_sequence(), making it print out no warnings
at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).

I wonder if I should file a ticket.

Stefano M. wrote in post #1011159:

x = 1
case x
when 0
when 1
when 1 + 0
when 1
end

is enough to fool iseq_set_sequence(), making it print out no warnings
at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).

I wonder if I should file a ticket.

It isn’t necessarily duplicated. You might have redefined Numeric#+ to
have a side-effect. It would be a silly thing to do, but Ruby doesn’t
know you’ve not done that.

Stefano M. wrote in post #1011271:

True, but there are still two identical “when 1”.
My point is that adding a “when 1 + 0” between them makes the warning go
away when it shouldn’t.

Or am I missing something?

Numeric#+ might have redefined Numeric#=== (which is the comparison
operator used by the case expression)

It’s an edge case, admittedly. But in general: any method call has the
ability to redefine any method.

On 7/17/11 9:25 PM, Brian C. wrote:

at all, even though we still do have a duplicate ‘when’ clause (let
alone that there are actually 3 duplicated clauses).

I wonder if I should file a ticket.

It isn’t necessarily duplicated. You might have redefined Numeric#+ to
have a side-effect. It would be a silly thing to do, but Ruby doesn’t
know you’ve not done that.

True, but there are still two identical “when 1”.
My point is that adding a “when 1 + 0” between them makes the warning go
away when it shouldn’t.

Or am I missing something?

On Jul 17, 2011, at 13:37 , Stefano M. wrote:

is enough to fool iseq_set_sequence(), making it print out no warnings
My point is that adding a “when 1 + 0” between them makes the warning go away
when it shouldn’t.

Or am I missing something?

I don’t think you’re missing anything. Considering the following causes
the warning as well:

x = 1
case x
when 1
when 0
when 1
end

I think you came up with a bug. Could you file a ticket on ruby-core’s
rubymine tracker?

On 7/18/11 4:22 AM, Ryan D. wrote:

On Jul 17, 2011, at 13:37 , Stefano M. wrote:

I think you came up with a bug. Could you file a ticket on ruby-core’s rubymine
tracker?

Sure thing, Ryan.
I’ll do that ASAP (tomorrow, most probably).

I still wasn’t 100% convinced, but since I’m fairly new to Ruby…