草庐IT

ruby-on-rails - 如何在正在运行的 ruby​​ 进程中更改 Date.today 的值

我知道这是个坏主意,但我有很多遗留代码,我想运行一些历史批处理作业。我不想更改系统日期,因为其他东西在同一系统上运行。有什么方法可以更改Date.today仅在给定进程的生命周期内返回的值。这里的想法是倒带并运行一些用于处理Date.today的旧批处理脚本。谢谢乔尔 最佳答案 您可以像Nikolaus向您展示的那样对Ruby进行猴子补丁,也可以使用TimeCopgem。它旨在使编写测试更容易,但您也可以在普通代码中使用它。#Setthetimewhereyouwanttogo.t=Time.local(2008,9,1,10,5,

ruby-on-rails - Ruby表达式 '-'后留空格的效果

今天我在我的Rails控制台中尝试了一些东西,这发生了,2.0.0p247:009>Date.today-29.days=>Fri,07Feb20142.0.0p247:010>Date.today-29.days=>Thu,09Jan2014我很困惑。我可以看到我缺少一些基本的东西。但这让我印象深刻!谁能解释为什么会这样? 最佳答案 实际发生的是这样的:Date.today(-29.days)#=>Fri,07Feb2014today有一个名为start的可选参数,默认为Date::ITALY。Anoptionalargument

ruby-on-rails - 将 Timecop gem 用于范围

我在Rails3.0应用程序中指定一个范围,如下所示:classDrawingList=?',Date.today,Date.today)end在我的规范中,我想做的是:beforedo@list=DrawingList.create#thingsthatincludebeginandenddatesendit"doesn'tfindanactivedrawingiftheyareoutofrange"dopending"reallyneedtofigureouthowtoworktimecopinthepresenceofscopes"Timecop.travel(2.days)pu

ruby - Date::today 未定义?

为什么只有Date类的某些方法没有显式加载:require'date'线?例如:irb(main):002:0>Date.todayNoMethodError:undefinedmethod`today'forDate:Classfrom(irb):2from/Users/mwlang/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in`'然后……irb(main):003:0>require'date'=>true导致...irb(main):004:0>Date.today=>#http://ruby-doc.org/stdlib-2.0/libdoc/

ruby - ruby Date.today 和 DateTime.now 的日期错误

我已经使用RVM安装了ruby​​-1.8.6-p383。系统ruby是1.9.1_p378-1我在使用ruby​​1.8时从Date.today和DateTime.now得到错误的日期。而Time.now是正确的:irb(main):002:0>DateTime.now.to_s=>"2126--1-10618T11:23:43+00:00"irb(main):004:0>Date.today.to_s=>"2126--1-10618"irb(main):005:0>Time.now=>ThuJan2811:55:27+00002010如果我切换到ruby​​1.9,一切都很好:ir

ruby - 在 Ruby 中迭代几天

我试过以下方法:defnext_seven_daystoday=Date.today(today..today+7).each{|date|putsdate}end但这只是给了我第一个和最后一个日期。我不知道如何获得介于两者之间的所有这些。我试着按照这里的例子:http://www.whynotwiki.com/Ruby_/_Dates_and_times 最佳答案 我想你想要更像这样的东西:defnext_seven_daystoday=Date.today(today..today+7).inject{|init,date|"#

ruby-on-rails - 为什么 Date.yesterday 也算作 Date.today?

我有以下模型和方法:classUserPriceDate.today)enddefself.yesterdaywhere(:purchase_date=>Date.yesterday)end为什么在我的表单上,如果我给日期选择字段:purchase_date1/4/2012(yesterday方法)它也算作今天(today方法),如果我给它1/5/2012(今天)它是零?附言我将Rails3.0.10与PostgreSQL结合使用。更新这是我的控制台返回:$railsconsole--sLoadingdevelopmentenvironmentinsandbox(Rails3.0.10

ruby - Date::today 未定义?

为什么只有Date类的某些方法没有显式加载:require'date'线?例如:irb(main):002:0>Date.todayNoMethodError:undefinedmethod`today'forDate:Classfrom(irb):2from/Users/mwlang/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in`'然后……irb(main):003:0>require'date'=>true导致...irb(main):004:0>Date.today=>#http://ruby-doc.org/stdlib-2.0/libdoc/

ruby-on-rails - Rails : Date. 今天 - 1.day

使用rails控制台,我只是被这个咬住了:假设今天是12月11日。Date.today-1.day#December10(nospaces)Date.today-1.day#December10(aspaceonbothsidesoftheminussign)Date.today-1.day#December11whaaaat?Date.today-5.days#Stilldecember11!有人能解释一下这是怎么回事吗?我有点担心这在代码中很容易被遗漏。关于如何对此进行编码还有其他建议吗? 最佳答案 您看到的差异是由ruby​​

ruby-on-rails - 计算不包括周末的天数

我在RubyonRails中创建了一个图书馆风格的系统,我试图想出一种方法来计算逾期天数,同时排除归还借来的项目的周末。现在我只是计算“dayslate”作为截止日期和项目实际退回日期之间的差异,但我想排除周末,因为项目只能在工作日退回。这是我第一次真正体验Ruby和Rails,所以如果我遗漏了一些明显的东西,我深表歉意。感谢大家提供的任何帮助。这是我的“返回”功能的代码:defreturn@product=Product.find(params[:id])today=Date.todaydayslate=today-@product.due_dateif@product.due_da