channa ly channa ly - 1 month ago 4
Ruby Question

Comparing variable string with a value cause a warning ruby 2

In my ruby 2.2.3 code I have a method in a class as follow

def partner?
auth_token.application.kind == 'partner'

Basically auth_token.application.kind will return a string but this function cause this:

Comparable#== will no more rescue exceptions of #<=> in the next release.

I am curious about this and I tried this instead

def partner?
'partner' == auth_token.application.kind

Then the warning is disappeared. Can anyone explain me the different between comparing string with variable on the left and right side of the expression above?


I guess in the first case auth_token.application.kind is returning an object that is not a standard Ruby String, and it is therefore executing that object's == method (which includes code to output the warning you see).

In the second case it is executing the 'partner' string's == method, which is just the standard Ruby String == method which does not generate this warning.

Note that in Ruby == is a method, if you were to write your example this way the reason for the difference would probably seem more clear: