2011-01-14 55 views
-3

我該如何檢查一個案件陳述的時間。怎麼做?最大和最小時間使用Ruby檢查case語句?

+2

您的問題並不十分清楚。您可能想要包含更多信息... – Peter 2011-01-14 05:59:14

+1

對不起,我的「愚蠢」的問題,但它是讓我瘋狂的RoR狂熱! :-) – user502052 2011-01-14 15:35:28

+0

@ user502052:問題不在於它是關於一個基本主題。問題是你沒有清楚地描述你想要的。 – 2011-01-17 06:45:03

回答

0

只需使用不頂部有一個定義的變量的版本...

t = event.time # arbitrary example. 

case 
when t <= Time.now 
    # Event is in the past. 
else 
    # Event is in the future. 
end 
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 

範圍與各種價值觀的工作。您可以使用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的爆發時間範圍,使例只適用紅寶石1.8.7。 Date示例雖然在兩個版本中都有效。在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