How many times have you found yourself facing the need to define equality check for your class objects? Uncountable times, right? I did too! As you'd expect, I implemented the eql?, ==, and hash methods for my class and everything was perfect in my own version of the world.
It was only until it was brought to my attention that I was merely checking the equality and not handling the cases which could throw exceptions and that would be really ugly if equality functions started throwing exceptions. What did I do? I added more checks in my implementation to avoid that problem and again my world was perfect! I was building my project over that and it was going smoothly, until at a point uniq method started acting weird. It would simply consider two unique elements same and would remove one of them.
After some digging into the problem and failing to find it, I tried implementing equality for this class as well but that didn't help! So I took a completely different approach to solve the problem at hand, this time without using uniq. Again I am building the project over that when in another class I find that include? method is reacting weird now!
This time instead of implementing the class logic again from the scratch, I was determined to find the root of all these problems. Finally after 2 hours of digging deep and closely inspecting everything, I realised that the equality function I implemented in the very first class was the real devil!
So in Ruby, when two objects are equal they have the same hash and so when we implement the hash method we do the same. What I misunderstood was that it was true the other way around as well, but that's not true! So if two objects have same hash its not necessary that they are equal. After I removed this check, everything went back to being perfect in my own little world! Though you should always keep this in mind, that nothing in this world is perfect :D
It was only until it was brought to my attention that I was merely checking the equality and not handling the cases which could throw exceptions and that would be really ugly if equality functions started throwing exceptions. What did I do? I added more checks in my implementation to avoid that problem and again my world was perfect! I was building my project over that and it was going smoothly, until at a point uniq method started acting weird. It would simply consider two unique elements same and would remove one of them.
After some digging into the problem and failing to find it, I tried implementing equality for this class as well but that didn't help! So I took a completely different approach to solve the problem at hand, this time without using uniq. Again I am building the project over that when in another class I find that include? method is reacting weird now!
This time instead of implementing the class logic again from the scratch, I was determined to find the root of all these problems. Finally after 2 hours of digging deep and closely inspecting everything, I realised that the equality function I implemented in the very first class was the real devil!
So in Ruby, when two objects are equal they have the same hash and so when we implement the hash method we do the same. What I misunderstood was that it was true the other way around as well, but that's not true! So if two objects have same hash its not necessary that they are equal. After I removed this check, everything went back to being perfect in my own little world! Though you should always keep this in mind, that nothing in this world is perfect :D
No comments:
Post a Comment