tags:

views:

89

answers:

3

I'm sure this is a basic question in Ruby:

Is there a way to check if

a == b

even if a is an integer and b is a string? I realize that I can do

a.to_s == b.to_s

but I'd like to know if there's some other, better way.

Edit: The question originally had a typo and said a.to_s and b.to_s which was edited after parsenome pointed out the typo.

A: 

I haven't run into one, and Rails gets tripped up by this regularly, so I suspect that there's no slick way to do it - you have to force the to_s.

a.to_s == b.to_s

might be more legible, though.

(question was editted to say == in the to_s compare after I added this)

edebill
sorry, yeah, the "and" was a typo. Yes, this is for rails, typical params and that kind of thing.
Yar
+4  A: 

I'm not sure that I understand the question. If I do understand it, I think you're trying to slip one by the interpreter:

telemachus ~ $ irb
irb(main):001:0> a = 1
=> 1
irb(main):002:0> b = '1'
=> "1"
irb(main):003:0> a == b
=> false

You can compare 1 and '1' all you like, but they aren't equal according to the way Ruby handles strings and numbers. In a nutshell, Ruby ain't Perl. (Edit: I should clarify. Clearly the number 1 is not the same thing as the string '1'. So it's not really a question of how Ruby handles them. If you compare them directly, they're just not the same. I just meant that Ruby doesn't do automatic conversions the way that Perl would. Depending on what language you come from and your attitude towards typing, this will make you happy or suprised or annoyed or some combination of these.)

Telemachus
yes, I am trying to slip one by the interpreter. Thanks for that.
Yar
I suppose then you could create a method `is_same_number_as?` to hide the conversion, but `to_s` seems straightforward (and easy) to me.
Telemachus
Yeah, I was just wondering if there's some operator that tries comparing them as, say, strings, regardless of their actual types.
Yar
@Yar: there may be a built-in bit of syntactic sugar for this, but if so I don't know it. My understanding is that Ruby *wants* to treat Fixnums and Strings differently, and it *won't* do automatic conversion for you, a la Perl.
Telemachus
Yeah, not sure either. I come from Java/C#, but later in PHP I got used to being loose with types.
Yar
There is no such operator. The thing is, these aren't just different types — they're different things altogether. They're not equivalent in any way, so making them equivalent in this way is rather odd. For example, the behavior of "1"+"1" is completely different from 1+1.
Chuck
Good point, Chuck. However, in PHP we play these games all the time. In Java or C# the thing blows up before you even compile (your IDE makes this happen). I still can't get used to Ruby's loose-strictness, but it's the way of the dynamic world, it seems...
Yar
+3  A: 

What about something like:

class Object
    def compare_as_strings(other)
      return self.to_s == other.to_s
    end
end

I hate extending something that fundamental, but this DOES work...

>> a = 1
=> 1
>> b = "1"
=> "1"
>> a.compare_as_strings(b)
=> true
edebill
that's cool, I'm just not into monkey-patching (fear of unintended consequences). I thought the language might have something built-in for this.
Yar
That's my feeling too, and why I didn't suggest overriding == :)
edebill
+1 Not (not not not) recommended, but does exactly what was asked for. It's only one small step away from VB's Evil Type Coercion, though, and millions of us spent a decade trying to avoid that one. Let's not bring it back!
Mike Woodhouse
That is true, Mike. Personally, I cannot stand the way Ruby pretends to be loose with types and then is actually very strict (dynamic and strong typed). I kind of prefer the fascism of Java/C# or the mayhem of PHP.
Yar
This also breaks if the two forms of the data don't agree on the string representation. 1 and 1.0, for instance. I'd expect them to be equal, but they'll fail with this method.
edebill