Ruby: How to store and display a day of the week?

后端 未结 4 1799
梦毁少年i
梦毁少年i 2020-12-31 08:58

I\'m relatively new to Ruby. I need to keep track of a day of the week on my activity model. The code that I have so far doesn\'t seem like the most elegant solution. I am w

4条回答
  •  情歌与酒
    2020-12-31 09:41

    Ruby's Date class already has a constant defined that holds the name of the days in an array: Date::DAYNAMES.

    For example, you could do something like:

    days = []
    Date::DAYNAMES.each_with_index { |x, i| days << [x, i] }
    

    I don't know if I'd necessarily say that this is better, but you can definitely leverage what is already available to you.

提交回复
热议问题