How to get the hash code of a time object in Ruby

Overview

The hash code of an object is a unique integer value. It differentiates an object from another. Objects with the same value have the same hash code.

Syntax

Error: Code Block Widget Crashed, Please Contact Support

t_o: The name of the time object whose hash code we want to obtain

Return value

It returns the hashcode of the time object.

Example

# create time objects
t1 = Time.now
t2 = Time.new()
t3 = Time.new(2020)
t4 = Time.new(2001, 11, 11)
# get hash code
# and print results
puts t1.hash
puts t2.hash
puts t3.hash
puts t4.hash
Get Hash Codes of Time Objects

Explanation

  • Line 2 to 4: We create some time objects, t1,t2, t3, and t4.
  • Line 9 to 12: We call the hash method on the time objects and print the results.

Free Resources