We can check whether or not two Time
objects are the same using the eq()?
method. It returns true
if two Time
objects have the same seconds. Otherwise, false
is returned.
t.eql?(other_t)
t
: This is a time instance that we want to compare with another time instance or object.
other_t
: This is the other time object or instance we want to compare with t
.
A boolean value is returned. true
is returned if the seconds of the t
and other_t
are the same.
# create time objectst1 = Time.nowt2 = Time.new(2023)t3 = Time.new(946702800)t4 = Time.new(946702800)# comparea = t1.eql?(t2)b = t2.eql?(t3)c = t3.eql?(t4)# print resultsputs a # falseputs b # falseputs c # true
Time.now
method.Time.new()
.Time.new()
method to create two time objects with the same number of seconds.t1
and t2
.t2
and t3
.t3
and t4
.Only line 9 returns true when the code is run because t3
and t4
both have the same number of seconds.