I don't understand the difference between struct and class equality check. Since both Struct and Class gets their #hash from Kernel but they seem to behave differently.
I know that instance.hash will produce a different result for each class instance. Struct instance has different ancestors [Customer, Struct, Enumerable, Object, Kernel, BasicObject] compare to Class instance [Foo, Object, Kernel, BasicObject]. What really caused each Class instance to have a different hash number to othe
Customer = Struct.new(:name, :phone, :address) do
end
class Foo
def initialize(the_name, phone, address)
@name = the_name
@phone = phone
@address = address
end
end
str_a = Customer.new('bond', 'ring', 'address')
str_b = Customer.new('bond', 'ring', 'address')
foo_a = Foo.new('bond', 'ring', 'address')
foo_b = Foo.new('bond', 'ring', 'address')
p str_a == str_b #true
p foo_a == foo_b #false
p str_a.hash # 4473040617195177332
p str_b.hash # 4473040617195177332
p foo_a.hash # -3118151143418428190
p foo_b.hash # -1042397847400824657
p str_a.method(:hash).owner #Kernel
p foo_a.method(:hash).owner #Kernel
both Struct and Class use Kernel for its hash_number generation. Why do a different instance of Class produce different hash int but Struct instance would produce the same hash int?