简体   繁体   English

数组中的Ruby日期比较

[英]Ruby date comparison in array

I have a ruby array end_dates that might look like... 我有一个红宝石数组end_dates可能看起来像...

end_dates =  [Sat, 14 Dec 2013, Sat, 14 Dec 2013, Sat, 14 Dec 2013, Fri, 13 Dec 2013, Sat, 14 Dec 2013, Sat, 14 Dec 2013, Sat, 14 Dec 2013]

...and I need a conditional statement that tells me weather a given date is greater than any of the dates in the array. ...并且我需要一个条件语句,告诉我天气给定的日期大于数组中的任何日期。 So it might look like... 所以看起来像...

my_date = Thursday, 12 Dec 2013 my_date = 2013年12月12日,星期四

compares my_date to all dates in the array and returns true if any dates are less than my_date 将my_date与数组中的所有日期进行比较,如果任何日期小于my_date,则返回true

if my_date > end_dates
  do stuff
end

Not as efficient as using any? 效率不如使用any? , but simpler: ,但更简单:

if my_date > end_dates.min
  ...
end

If you keep the min value somewhere, and reuse it: 如果将最小值保留在某个地方,然后重复使用:

min = end_dates.min
...
if my_date > min
  ...
end

then it would not be inefficient. 那就不会低效率。

if end_dates.any? { |end_date| end_date < my_date } # do stuff end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM