红宝石 |时间 <=> 方法
这是 Ruby 中的一个内置方法,根据时间是小于、等于还是大于其他时间返回 -1、0、1。如果两次不可比,则返回 nil。
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
示例 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
输出:
-1
示例 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
输出:
1