Max和Min Time检查在使用Ruby的情况下进行语句?
-
11-10-2019 - |
题
我该如何查看案例语句的时间。怎么做?
解决方案
只需使用顶部没有定义变量的版本...
t = event.time # arbitrary example.
case
when t <= Time.now
# Event is in the past.
else
# Event is in the future.
end
其他提示
使用范围:
case time
when (Time.now - 60)..(Time.now) then puts 'within the last minute'
when (Time.now - 3600)..(Time.now) then puts 'within the last hour'
end
范围与各种值一起工作。您可以使用 Date
也:
case date
when (Date.today - 1)..(Date.today) then puts 'less than a day ago'
when (Date.today - 30)..(Date.today) then puts 'less than a month ago'
end
更新:Ruby 1.9破坏了时间范围,因此该示例仅在Ruby 1.8.7中起作用。日期示例可在两个版本中使用。在1.9中,您可以使用此代码匹配一个时间:
case time.to_i
when ((Time.now - 60).to_i)..(Time.now.to_i) then puts 'within the last minute'
when ((Time.now - 3600).to_i)..(Time.now.to_i) then puts 'within the last hour'
end
不隶属于 StackOverflow