samedi 28 février 2015

Reverse vs Reverse! in Ruby when comparing palindrome

I know that reverse creates a new string with the characters of the string in reverse and that reverse! mutates (reverses) the current string in place. My question is why, when for example testing for a palindrome, this occurs?:



a = "foobar"
a == a.reverse # => false
a == a.reverse! # => true


Is it because it is the same object in memory, therefore == just checks if they have the same memory location?


Thanks!


Aucun commentaire:

Enregistrer un commentaire