Open In App

Ruby | Time <=> method

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The is an inbuilt method in Ruby returns -1, 0, 1 depending on whether time is less than, equal to, or greater than other time. It is returns nil if two times are incomparable.

Syntax: time <=> otherTime

Parameters: The function accepts no parameter

Return Value: It returns -1, 0, 1 depending on whether time is less than, equal to, or greater than other time

Example 1:




# Ruby code for <=> method
  
# Include Time
require 'time'
  
# Declaring time 
a = Time.new(1993, 02, 24, 12, 0, 0, "+09:00")
  
b = a + (60*60)
  
# Prints -1
puts a <=> b


Output:

-1

Example 2:




# Ruby code for <=> method
  
# Include Time
require 'time'
  
# Declaring time 
a = Time.new(1993, 02, 24, 12, 0, 0, "+09:00")
  
b = a + (60*60)
  
# Prints 1
puts b <=> a


Output:

1

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads