简体   繁体   English

检查该值是否在最近的数组元素之间。 (红宝石)

[英]Check if the value is in range between closest array's elements. (ruby)

I need to know how would I check if my given value is between two closest array's members. 我需要知道如何检查给定的值是否在两个最接近的数组成员之间。 For example I have an array of dates with the date of week start in given period of time. 例如,我有一个日期数组,其中星期几在给定的时间段内开始。 And I need to check if my given date is in one of its week. 而且我需要检查给定的日期是否在一周中的某一天。 For example: 例如:

  1. 2015-11-02 2015-11-02
  2. 2015-11-09 2015-11-09
  3. 2015-11-16 2015-11-16
  4. 2015-11-23 2015-11-23

And my given value is 2015-11-11 for example. 我给定的值是2015-11-11。 How should I check if it is one of these weeks date? 我应该如何检查是否是这几周的日期之一? Thanks for help. 感谢帮助。

%w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).any? do |date| 
  date.to_date.cweek == Date.today.cweek
end

And here is what this does: First, you have an array of strings, you use any? 这是这样做的:首先,您有一个字符串数组,使用any?字符串any? to loop through it and check if any fulfils a requirement, then you cast you date strings into actual dates, and cweek gives you the number of a week in the year. 遍历它并检查是否满足要求,然后将日期字符串转换为实际日期,而cweek则为您提供一年中的星期数。 Date.today gives you today's date. Date.today给您今天的日期。

Instead of Date.today.cweek you can use '2015-11-11'.to_date.cweek . 您可以使用'2015-11-11'.to_date.cweek代替Date.today.cweek

The loop above returns boolean; 上面的循环返回布尔值; you could also get an array of values that fulfil a condition like this: 您还可以获得满足以下条件的值数组:

new_array = %w(2015-11-02 2015-11-09 2015-11-16 2015-11-23).map do |date| 
  date.to_date.cweek == '2015-11-11'.to_date.cweek
end.compact

Resources: 资源:


UPDATE 更新

If you want to get from the database only records with a date from particular week, this is how you could do it: 如果您只想从数据库中获取具有特定星期中某个日期的记录,则可以这样做:

my_date = '2015-11-11'.to_date
matching_records = MyResource.where( date: my_date.beginning_of_week..my_date.end_of_week )

The assumptions are that you have a model MyResource , and that it has a column date . 假设您有一个MyResource模型,并且有一个列date What this does is returns a relation with all the records that have dates from the same week as my_date . 这样做是返回与日期与my_date同一周的所有记录的关系。

假设您的日期数组已排序:

date >= dates.first && date <= dates.last

If you're dealing with strings, you can "require 'date'" and transform your strings to dates ("Date.parse('2001-02-03')"). 如果要处理字符串,则可以“需要'date'”并将字符串转换为日期(“ Date.parse('2001-02-03')”)。

As others have suggested, you can then see if your date is between the first and last entry of your list. 正如其他人所建议的,然后您可以查看您的日期是否在列表的第一项和最后一项之间。

If the real list is sorted and each entry is one week apart, then you can easily find where in the list your guy is. 如果对真实列表进行了排序,并且每个条目相隔一个星期,那么您可以轻松地找到您的家伙在列表中的位置。

Eg, say the list is [date_0, date_1, date_2, ..., date_k] (all 1 week apart), and you're given a test_date between date_0 and date_k. 例如,假设列表是[date_0,date_1,date_2,...,date_k](相隔1周),并且给您一个介于date_0和date_k之间的test_date。 Then (test_date.jd - date_0.jd)/7 gives you the index of the date in your list that is <= test_date. 然后(test_date.jd-date_0.jd)/ 7为您提供列表中日期的索引,即<= test_date。

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

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