检查时间在9.30到4之间

我有一个错误地产生它的代码,我认为必须有一个更好的方法来检查时间>上午9点30分和时间<4 pm。任何想法都是高度适用的。

def checkTime goodtime=false if(Time.now.hour>9 and Time.now.min>30) then if(Time.now.hour9 and Time.now.hour<16 then goodtime=true else # do nothing end return goodtime end 

 t = Time.now Range.new( Time.local(t.year, t.month, t.day, 9), Time.local(t.year, t.month, t.day, 16, 30) ) === t 
 def check_time(t=Time.now) early = Time.new(t.year, t.month, t.day, 9, 30, 0, t.utc_offset) late = Time.new(t.year, t.month, t.day, 16, 0, 0, t.utc_offset) t.between?(early, late) end 

只是:

 def checkTime return ((Time.now.hour * 60) + Time.now.min) >= 570 && ((Time.now.hour * 60) + Time.now.min) < 960 end 

在混合版本环境中工作,需要这个确切的方法,这就是我在代码中添加的内容:

 if RUBY_VERSION == "1.9.3" def check_time(starthour,endhour,t=Time.now) early = Time.new(t.year, t.month, t.day, starthour, 0, 0) late = Time.new(t.year, t.month, t.day, endhour, 0, 0) t.between?(early, late) end elsif RUBY_VERSION == "1.8.7" def check_time(starthour,endhour,t=Time.now) early = Time.local(t.year, t.month, t.day, starthour, 0, 0) late = Time.local(t.year, t.month, t.day, endhour, 0, 0) t.between?(early, late) end end