Skip to content
Related Articles

Related Articles

Ruby | Time <=> method

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Jan, 2020
View Discussion
Improve Article
Save Article

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!