草庐IT

real_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-on-rails - 使用 Date::DAYNAMES 在 Rails 中显示一周中的几天列表

我无法在表单中显示一周中的几天列表。我遇到了错误undefinedmethod`Sunday'for#但是如果我只是显示,它会给我一个列表Sunday,Monday,Tuesday,etc...toSaturday我在这里做错了什么?谢谢 最佳答案 替换与 关于ruby-on-rails-使用Date::DAYNAMES在Rails中显示一周中的几天列表,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com

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 - 导轨 : seeding database data and date formats

我正在尝试使用db/seed.rb文件和rakedb:seed命令将一些数据播种到我正在开发的Rails3应用程序中。我播种的数据涉及一个名为Meeting的数据库表,该表具有三列:字符串标题、日期时间开始日期、日期时间结束日期。我尝试插入的日期时间采用“mm/dd/yyyyhh:mm”格式——例如。“01/02/200313:23”=2003年1月2日下午1:23。但是,DateTime.parse()始终会出现“无效日期”错误——因为它试图以“dd/mm/yyyy”格式解析日期。根据我的谷歌搜索,我被引导相信在解析DateTime对象时,编译器会查看传入的字符串并进行一些仓促的模式

ruby - 在 Ruby 中,为什么等于 nil ("Date.new == nil") 返回 nil?

今天写一些rspec时,我遇到了一些意外的行为,将日期(和时间)实例与nil进行比较。这是一个使用原始ruby​​的示例(没有Rails或其他库):user@MacBook-Work~$ruby-vruby1.8.7(2008-08-11patchlevel72)[universal-darwin10.0]user@MacBook-Work~$irb>>1==nil=>false>>"string"==nil=>false>>:sym==nil=>false>>false==nil=>false>>[]==nil=>false>>{}==nil=>false>>Proc.new{}==

ruby-on-rails - Date 与 ActiveSupport::TimeWithZone 的比较失败

我的Waiver模型上有一个age方法,如下所示:defage(date=nil)ifdate.nil?date=Date.todayendage=0unlessdate_of_birth.nil?age=date.year-date_of_birth.yearage-=1ifdate然后我有一个看起来像这样的规范:it"calculatestheproperage"dowaiver=FactoryGirl.create(:waiver,date_of_birth:12.years.ago)waiver.age.should==12end当我运行这个规范时,我得到Comparisono

ruby - 使 Date#parse 采用美国格式而不是欧盟格式

在1.9.2p0中,Date#parse采用UE格式。如果您不相信我,请查看format.rb,第1042行。无论如何,我怎样才能让它采用美国格式,以便:>Date.parse("10/4/2010")=>Mon,04Oct2010而不是4月10日。我已经试过了:classDatedef_parse_eu(str,e)_parse_us(str,e)endend但运气不好。还有其他想法吗? 最佳答案 Date.strptime是你想要的但不幸的是它看起来不像文档有日期格式字符串。我得到以下基于格式字符串的谷歌搜索的工作:1.9.2>

ruby - 寻找 "real"使用延续的例子

我试图掌握延续的概念,我从Wikipediaarticle中找到了几个像这样的小教学示例。:(definethe-continuation#f)(define(test)(let((i0));call/cccallsitsfirstfunctionargument,passing;acontinuationvariablerepresentingthispointin;theprogramastheargumenttothatfunction.;;Inthiscase,thefunctionargumentassignsthat;continuationtothevariablethe

ruby - 为什么 Date 在需要之前就存在于 Ruby 中?

在Ruby中,我希望一个不需要的类会引发“未初始化常量”错误。例如,CSV就是这种情况。但是,Date的行为很奇怪:它可用,但显然不起作用,直到需要它为止。~:irb>>Date.new(2012,7,24)ArgumentError:wrongnumberofarguments(3for0)>>require'date'=>true>>Date.new(2012,7,24)=>#如何解释这种行为? 最佳答案 我相信date不是来自irb,而是来自rubygems,特别是thefile其中定义了Gem::Specification: