# ===.() not being called in case statement

**URL:** <https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058>\
**Category:** Rails\
**Created:** [April 4, 2007, 6:35am UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058 "2007-04-04T06:35:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![monki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@monki](https://www.ruby-forum.com/u/monki)\
**Post date:** [April 4, 2007, 6:35am UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058/1 "2007-04-04T06:35:41Z")

</div>

I’ve been trying to get the open\_id\_authentication plugin to play nice  
in my applications, and after a few hours of trial and error I have it  
running. I had to make a number of changes from the README text, and  
I’m trying to pin down what is going on with the case statement for  
status. Here is a simplified version that still exhibits the problem:

* * *

class Result  
def self.  
new(code)  
end

def initialize(code)  
@code = code  
end

def ===(code)  
puts ‘===’  
@code == code  
end  
end

result = Result[:successful]

case result  
when :successful  
puts ‘successful’  
else  
puts ‘unsuccessful’  
end

* * *

# My expected output:

successful

But the actual output is:  
unsuccessful

My understanding is that case statements use the === function for  
comparisons. And in some cases they certainly do (I have seen it work  
on other examples). It would appear that in this case the === is not  
even being called at all (I tried to pin down what function was being  
called, but didn’t have much luck).

Could somebody please explain to me why this doesn’t work or how one  
might fix it?

---

<div class="post-metadata">

**Author:** ![monki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@monki](https://www.ruby-forum.com/u/monki)\
**Post date:** [April 4, 2007, 9:56am UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058/2 "2007-04-04T09:56:45Z")

</div>

monki wrote:

> ```
> new(code)
> 
> ```
> 
> ## end
> 
> on other examples). It would appear that in this case the === is not  
> even being called at all (I tried to pin down what function was being  
> called, but didn’t have much luck).
> 
> Could somebody please explain to me why this doesn’t work or how one  
> might fix it?

The === function is called on the object in the when condition, with  
the case object as the parameter, not the other way around. It won’t  
work with the simplified code you’ve presented, but you’ll probably  
have to write something like:

result = Result[:successful]

case result  
when Result[:successful] then puts ‘successful’  
else puts ‘unsuccessful’  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![monki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@monki](https://www.ruby-forum.com/u/monki)\
**Post date:** [April 4, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058/3 "2007-04-04T16:38:19Z")

</div>

> The === function is called on the object in the when condition, with  
> the case object as the parameter, not the other way around. It won’t  
> work with the simplified code you’ve presented, but you’ll probably  
> have to write something like:  
> That would certainly explain some things, and why it works in the  
> examples I had seen.

> result = Result[:successful]
> 
> case result  
> when Result[:successful] then puts ‘successful’  
> else puts ‘unsuccessful’  
> end  
> I figured it could be done this way, but it seemed wasteful to create  
> a new object instance just to compare against.

Not exactly sure what I want to do with it at present, but this  
definately makes it much clearer what is going on.

Thanks for the insight.

---

<div class="post-metadata">

**Author:** ![monki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@monki](https://www.ruby-forum.com/u/monki)\
**Post date:** [April 4, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058/4 "2007-04-04T16:53:02Z")

</div>

monki wrote:

> My understanding is that case statements use the === function for  
> comparisons. And in some cases they certainly do (I have seen it work  
> on other examples). It would appear that in this case the === is not  
> even being called at all (I tried to pin down what function was being  
> called, but didn’t have much luck).
> 
> Could somebody please explain to me why this doesn’t work or how one  
> might fix it?

Can’t do it for the reasons listed above but this looks almost as  
pretty  
irb(main):002:0\> class Result  
irb(main):003:1\> def self.  
irb(main):004:2\> new(code)  
irb(main):005:2\> end  
irb(main):006:1\> def initialize(code)  
irb(main):007:2\> @code = code  
irb(main):008:2\> end  
irb(main):009:1\> def to\_sym  
irb(main):010:2\> @code  
irb(main):011:2\> end  
irb(main):012:1\> end  
=\> nil  
irb(main):013:0\> result = Result[:successfull]  
=\> #\<Result:0x360a88 @code=:successfull\>  
irb(main):014:0\> case result.to\_sym  
irb(main):015:1\> when :successfull  
irb(main):016:1\> puts ‘yay’  
irb(main):017:1\> else  
irb(main):018:1\* puts ‘nay’  
irb(main):019:1\> end  
yay  
=\> nil

---

<div class="post-metadata">

**Author:** ![monki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@monki](https://www.ruby-forum.com/u/monki)\
**Post date:** [April 4, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/not-being-called-in-case-statement/91058/5 "2007-04-04T21:25:01Z")

</div>

In the end I think it makes the most sense to just add an attr\_reader,  
and do away with the non-working === (even though it would be nice to  
handle the comparison automagically)

* * *

class Result  
attr\_reader :code

def self.  
new(code)  
end

def initialize(code)  
@code = code  
end  
end

result = Result[:successful]

case result.code  
when :successful  
puts ‘successful’  
else  
puts ‘unsuccessful’  
end

* * *

Which, as one might expect, gives you:  
successful

In any event it is quite good to know the order of operations for  
case. I couldn’t find anything that described the order and it makes  
a big difference when you are comparing objects of different classes.
