Ruby on rails 如何使用Ruby/Rails获得本月的第一个星期四?

Ruby on rails 如何使用Ruby/Rails获得本月的第一个星期四?,ruby-on-rails,ruby,Ruby On Rails,Ruby,下面的Ruby代码让我知道每个月的第一天: require 'active_support/all' # get the date at the beginning of this month date = Date.today.beginning_of_month # get the first day of the next 5 months 5.times do |num| date = date.next_month p date end 其中: => Fri, 01

下面的Ruby代码让我知道每个月的第一天:

require 'active_support/all'

# get the date at the beginning of this month
date = Date.today.beginning_of_month

# get the first day of the next 5 months
5.times do |num|
  date = date.next_month
  p date
end
其中:

=> Fri, 01 Aug 2014
=> Mon, 01 Sep 2014
=> Wed, 01 Oct 2014
=> Sat, 01 Nov 2014
=> Mon, 01 Dec 2014
但是我怎么才能得到每个月的第一个星期四呢?i、 e

=> Thu, 07 Aug 2014
=> Thu, 04 Sep 2014
=> Thu, 02 Oct 2014
=> Thu, 06 Nov 2014
=> Thu, 04 Dec 2014
这是我的方式:

def first_thursday
  date = Date.today.beginning_of_month
  date += 1 until date.wday == 4
  date
end

first_thursday # => Thu, 03 Jul 2014 

您可以使用以下内容:

def first_thursday(months_ahead)
  start_of_month = months_ahead.months.from_now.beginning_of_month.to_date
  start_of_month += (4 - start_of_month.cwday) % 7
end

first_thursday 1
=> Thu, 07 Aug 2014
first_thursday 2
=> Thu, 04 Sep 2014
只是为了好玩

class Date
  def skip_to_thursday
    # given current weekday, how many days we need to add for it to become thursday
    # for example, for monday (weekday 1) it's 3 days

    offset = lambda {|x| (4-x) % 7 }    
    self + offset[wday]
  end
end


# get the date at the beginning of this month
date = Date.today.beginning_of_month

date.skip_to_thursday # => Thu, 03 Jul 2014

无需迭代或条件,只需在下周四之前获得所谓的天数增量:

#4 is thursday because wday starts at 0 (sunday)

date = Date.today.beginning_of_month
date += (4 - date.wday) % 7
p date
=> Thu, 03 Jul 2014
我认为:

date_begin = Date.today.beginning_of_month
date_end = date_begin + 5.month
[*date_begin..date_end].select(&:thursday?).uniq(&:month)
=> [Thu, 03 Jul 2014, Thu, 07 Aug 2014, Thu, 04 Sep 2014, Thu, 02 Oct 2014, Thu, 06 Nov 2014]

我遇到了这个问题,因为我需要构建一个
重复发生的\u事件
功能。我改变了一些变量,以找到第一个星期四,但它也显示了如果你有一周和一周中的某一天计数,你可以如何进化出答案来找到第二个或第三个星期四(或一周中的任何一天)

def find_thursday
  start_of_month = DateTime.now.beginning_of_month
  month_day = nil
  loop do
    month_day = start_of_month += 1.day
    break if month_day.wday == find_weekday("Thu")
  end
  return month_day
end


def find_weekday
  d = default_weekdays.find { |d| d[:day] == start_date.strftime("%a") }
  d[:count]
end


def default_weekdays
  return [
    { day: 'Sun', count: 0 },
    { day: 'Mon', count: 1 },
    { day: 'Tue', count: 2 },
    { day: 'Wed', count: 3 },
    { day: 'Thu', count: 4 },
    { day: 'Fri', count: 5 },
    { day: 'Sat', count: 6 },
  ]
end

7月或9月不工作。事实上,这可能是纯粹的运气,它确实为八月:)我是如此愚蠢。。谁还不明白。。那个杂烩在那里干什么。。你能帮我说几句话吗?:-)尝试一个时髦的lambda:
offset=lambda{x |(4-x)%7}
这样你就可以使用
offset[2]#=>2
@xlembouras:awww,聪明:)或者
offset=[4,3,2,1,7,6]
offset.index(wday)
右键。这就是第一个版本中的内容。查看编辑历史:)在haskell中,这可能非常有效。在ruby中-不太多:)
.map{x | x如果x.thready?}.compact
=>
。选择(&:thready?