Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Ruby:将日期作为小数转换为日期作为名称大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以快速将strftime(“%u”)值转换为strftime(“%A”),或者我是否需要构建等价散列,如{“Monday”=> 1,………“星期日”=> 6}

我有一个数组,有一天作为十进制值

class_index=[2,6,7]

我想循环遍历这个数组来构建和数组这样的天名

[nil,"Tuesday",nil,"Saturday","Sunday"]

所以我能做到

class_list=[]
class_index.each do |x|
  class_list[x-1] = convert x value to day name
end

这甚至可能吗?

解决方法

怎么样:
require "date"
datetiR_215_11845@e.parse("Wednesday").wday # => 3

哦,我现在看到你扩大了你的问题.怎么样:

[2,7].inject(Array.new(7)) { |memo,obj| memo[obj-1] = Date::DAYNAMES[obj%7]; memo }

让我解释一下:

input = [2,7]
empty_array = Array.new(7) # => [nil,nil]
input.inject(empty_array) do |memo,obj| # loop through the input,and
                                         # use the empty array as a 'memo'
  day_name = Date::DAYNAMES[obj%7]       # get the day's name,modulo 7 (Sunday = 0)
  memo[obj-1] = day_name                 # save the day name in the empty array
  memo                                   # return the memo for the next iteration
end

Ruby的美丽.

大佬总结

以上是大佬教程为你收集整理的Ruby:将日期作为小数转换为日期作为名称全部内容,希望文章能够帮你解决Ruby:将日期作为小数转换为日期作为名称所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。