Ruby,检查日期是否是周末? [英] Ruby, check if date is a weekend?

查看:34
本文介绍了Ruby,检查日期是否是周末?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails 应用程序中有一个 DailyQuote 模型,它具有股票的日期价格.数据库中的数据已为此模型捕获,包括周末.周末价格值已设置为 0.

I have a DailyQuote model in my rails application which has a date and price for a stock. Data in the database has been captured for this model including weekends. The weekend price values have been set as 0.

我想将周六和周日的所有周末价格更改为周五的价格.在 Ruby 中执行此操作的最佳方法是什么?确定日期是星期六还是星期日并将其值更改为那个周末的星期五?

I want to change all the weekend prices for Saturday and Sunday to whatever the price was on Friday. What is the best way to do this in Ruby? To identify if a date falls on a Sat or Sun and change its value to the Fri of that weekend?

推荐答案

require 'date'
today = Date.today
ask_price_for = (today.wday == 6) ? today - 1 : (today.wday == 0) ? today - 2 : today

require 'date'
today = Date.today
ask_price_for = (today.saturday?) ? today - 1 : (today.sunday?) ? today - 2 : today  

ask_price_for 现在包含您想要询问价格的日期.

ask_price_for now holds a date for which you would want to ask the price for.

获取与您的日期相对应的实际价格取决于您的模型和您的 ORM 库(即 ActiveRecord).

Getting the actual price which is corresponding to you date depends on your Model and your ORM-Library (i.e. ActiveRecord).

这篇关于Ruby,检查日期是否是周末?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆