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

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:

monki wrote:

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.(code)     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?

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

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.

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)