Open In App

Ruby | Time <=> method

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
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: CPP
# 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: CPP
# 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

Similar Reads