A hash value is a unique value that differentiates an object’s value from others. Objects with the same value have the same hash. We can use the
hash attribute to get the hash value of a float in Ruby.
The value returned is the hash integer value of the float value
# create some float values f1 = 70.2323 f2 = 1.0/0.0 f3 = -4.3334 f4 = f1/1.0 # get hash values puts f1.hash puts f2.hash puts f3.hash puts f4.hash
hashmethod. Then we print the results to the console.
As we can see, float
f4 both have the same hash value because their value is the same.
View all Courses