gpt4 book ai didi

Ruby 确定季节(秋季、冬季、 Spring 或夏季)

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:39 26 4
gpt4 key购买 nike

我正在编写一个脚本,该脚本应该根据日期范围确定一年中的“季节”:

例如:

January 1 - April 1: Winter
April 2 - June 30: Spring
July 1 - September 31: Summer
October 1 - December 31: Fall

我不确定执行此操作的最佳方式(或最佳 ruby​​ 方式)。还有其他人遇到如何做到这一点吗?

最佳答案

9 月 31 日?

正如 leifg 所建议的,这里是代码:

require 'Date'

class Date

def season
# Not sure if there's a neater expression. yday is out due to leap years
day_hash = month * 100 + mday
case day_hash
when 101..401 then :winter
when 402..630 then :spring
when 701..930 then :summer
when 1001..1231 then :fall
end
end
end

定义后,将其命名为 e.g.像这样:

d = Date.today
d.season

关于Ruby 确定季节(秋季、冬季、 Spring 或夏季),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com