Ruby 如何对条件进行编码:';此字符串能否转换为日期类型';?(红宝石)
我可以用ruby编写这个条件吗?我需要知道字符串是否可以转换为日期变量。唯一的方法是Date.parse(“我的字符串”)和exception。还有别的办法吗Ruby 如何对条件进行编码:';此字符串能否转换为日期类型';?(红宝石),ruby,date,Ruby,Date,我可以用ruby编写这个条件吗?我需要知道字符串是否可以转换为日期变量。唯一的方法是Date.parse(“我的字符串”)和exception。还有别的办法吗 date_scraped_from_the_net = "20 Dec 2009" or it could be "today" if date_scraped_from_the_net is not a date type needs_to_be_updated = true end 如果您需要复杂的日期解析,我会尝试 但是,
date_scraped_from_the_net = "20 Dec 2009" or it could be "today"
if date_scraped_from_the_net is not a date type
needs_to_be_updated = true
end
如果您需要复杂的日期解析,我会尝试 但是,如果我理解您的具体问题,您将希望使用
rescue
来使用在条件语句中引发错误的方法:
if (Date.parse(date_scraped_from_the_net) rescue nil)
needs_to_be_updated = true
end
@弗洛伊德:你的代码对我不起作用。我想会有其他更简单的方法。看起来很好。它正在工作。谢谢。如果其他人有任何想法,我会等一会儿,否则我会接受你的回答。