-3

ケースステートメントで時間を確認するものです。実行する方法?

4

2 に答える 2

3

使用範囲:

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

範囲はあらゆる種類の値で機能します。Dates も使用できます。

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
于 2011-01-14T06:05:03.220 に答える
0

上部に変数が定義されていないバージョンを使用するだけです...

t = event.time  # arbitrary example.

case
when t <= Time.now
  # Event is in the past.
else
  # Event is in the future.
end
于 2011-01-14T05:57:42.210 に答える