Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

float
ruby
hash
communitycreator

How to get the hash value of a float in Ruby?

okwudili onyejiaku

Overview

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.

Syntax

flt.hash

Return value

The value returned is the hash integer value of the float value flt.

Code example

# 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
Getting the hash values of float values in Ruby

Explanation

  • Lines 2-5: We create some float values.
  • Lines 8-11: We get the hash values of the float values using the hash method. Then we print the results to the console.

As we can see, float f1 and f4 both have the same hash value because their value is the same.

RELATED TAGS

float
ruby
hash
communitycreator
RELATED COURSES

View all Courses

Keep Exploring