如何通过ruby的case语句从哈希中获取特定值?

更新于
2026-10-09 21:13:56
2阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计460个文字,预计阅读时间需要2分钟。

如何通过ruby的case语句从哈希中获取特定值?

我在尝试编写一个函数,该函数使用日期的日期编号,例如,今天(3月29日)是一年中的第88天。然后它返回一个包含月份和日期日期的哈希:{month: March, day: 29}。我无法清除这段代码的障碍。

我正在尝试编写一个函数,该函数采用日期的日期编号,例如,今天(3月29日)是一年中的第88天.然后它返回一个包含月份和月份日期的哈希:

{"month" => "March, "day" => 29}

我无法弄清楚这段代码有什么问题,但它总是返回nil.有什么想法吗?我使用的是Ruby 1.8.7 p358.

def number_to_date(days) date = case days when days <= 31 then {"month" => "January", "day" => days} when days <= 59 then {"month" => "February", "day" => (days - 31)} when days <= 90 then {"month" => "March", "day" => (days - 59)} when days <= 120 then {"month" => "April", "day" => (days - 90)} when days <= 151 then {"month" => "May", "day" => (days - 120)} when days <= 181 then {"month" => "June", "day" => (days - 151)} when days <= 212 then {"month" => "July", "day" => (days - 181)} when days <= 243 then {"month" => "August", "day" => (days - 212)} when days <= 273 then {"month" => "September", "day" => (days - 243)} when days <= 304 then {"month" => "October", "day" => (days - 273)} when days <= 334 then {"month" => "November", "day" => (days - 304)} when days <= 365 then {"month" => "December", "day" => (days - 334)} end return date end 如果要在每个when子句中使用表达式,则需要使用裸case语句.否则,Ruby将调用(days< = 31)=== days,这将永远不会成立.

def number_to_date(days) date = case when days <= 31 then {"month" => "January", "day" => days} when days <= 59 then {"month" => "February", "day" => (days - 31)} # ... end return date end

然而,这种实现忽略了闰日,这样做似乎更简单,更正确:

def number_to_date(days) date = Date.ordinal(Date.today.year, days) {"month" => Date::MONTHNAMES[date.month], "day" => date.day} end

如何通过ruby的case语句从哈希中获取特定值?

本文共计460个文字,预计阅读时间需要2分钟。

如何通过ruby的case语句从哈希中获取特定值?

我在尝试编写一个函数,该函数使用日期的日期编号,例如,今天(3月29日)是一年中的第88天。然后它返回一个包含月份和日期日期的哈希:{month: March, day: 29}。我无法清除这段代码的障碍。

我正在尝试编写一个函数,该函数采用日期的日期编号,例如,今天(3月29日)是一年中的第88天.然后它返回一个包含月份和月份日期的哈希:

{"month" => "March, "day" => 29}

我无法弄清楚这段代码有什么问题,但它总是返回nil.有什么想法吗?我使用的是Ruby 1.8.7 p358.

def number_to_date(days) date = case days when days <= 31 then {"month" => "January", "day" => days} when days <= 59 then {"month" => "February", "day" => (days - 31)} when days <= 90 then {"month" => "March", "day" => (days - 59)} when days <= 120 then {"month" => "April", "day" => (days - 90)} when days <= 151 then {"month" => "May", "day" => (days - 120)} when days <= 181 then {"month" => "June", "day" => (days - 151)} when days <= 212 then {"month" => "July", "day" => (days - 181)} when days <= 243 then {"month" => "August", "day" => (days - 212)} when days <= 273 then {"month" => "September", "day" => (days - 243)} when days <= 304 then {"month" => "October", "day" => (days - 273)} when days <= 334 then {"month" => "November", "day" => (days - 304)} when days <= 365 then {"month" => "December", "day" => (days - 334)} end return date end 如果要在每个when子句中使用表达式,则需要使用裸case语句.否则,Ruby将调用(days< = 31)=== days,这将永远不会成立.

def number_to_date(days) date = case when days <= 31 then {"month" => "January", "day" => days} when days <= 59 then {"month" => "February", "day" => (days - 31)} # ... end return date end

然而,这种实现忽略了闰日,这样做似乎更简单,更正确:

def number_to_date(days) date = Date.ordinal(Date.today.year, days) {"month" => Date::MONTHNAMES[date.month], "day" => date.day} end

如何通过ruby的case语句从哈希中获取特定值?