Determining if two objects are "equal"

@another_fen - Yes and no. Based on my tests, for objects without a custom marshal_dump defined, it appears the order of serialization is based on the creation order of the instance variables the very first time they were created within any given class during this run. This also applies to objects reconstituted with Marshal.load() - if this was the first time certain instance variables were created, they will be "remembered" by Marshal for this run. Therefore, even if you load two objects from files from runtimes that had a different instance variable creation order (and therefore a different serialization order), the string returned by Marshal.dump() for both objects will be the same if both original objects had the same attributes, regardless of order.


If you want things to be serialized and loaded in a specific, non changing order, you can define your own marshal_dump and marshal_load instance methods, but this shouldn't be necessary unless you need Marshal support for a class that doesn't have it by default or you need to analyze the byte stream yourself.


@shaz


It should be also be for this method that a non-existent instance variable and nil value for a variable have two different serialization values, so omitting variables will cause a different serialization output even though the "value" of a non initialized instance variable is still nil. But since you said you weren't too concerned with lazy evaluation, this probably won't be an issue either.
I've covered the case where classes are made up of attributes, and some of those are smaller classes. Not being able to do a == b is what prompted this whole post in the first place. I THINK I've got it sorted in most cases - preliminary testing returns good results.


I've also catered for the possibility of nil values and of attributes that exist in one but not the other.


I had written up an overview of what I've done so far and am currently testing, but when I posted, I somehow lost it when the screen refreshed. I don't have time to write it up again. The weekend will give me another opportunity 🙂

Original thread: https://forums.rpgmakerweb.com/threads/determining-if-two-objects-are-equal.31635/