jjzjj

Get-Date

全部标签

ruby - Ruby 的 Object#const_get 是如何工作的?

我最近发现Ruby(2.2.1)有一些“有趣”的行为。moduleFooclassFooendclassBarendendFoo.const_get('Foo')#=>Foo::FooFoo.const_get('Bar')#=>Foo::BarFoo.const_get('Foo::Foo')#=>FooFoo.const_get('Foo::Bar')#=>NameError:uninitializedconstantFoo::Foo::BarFoo.const_get('Foo::Foo::Bar')#=>Foo::BarFoo.const_get('Foo::Foo::Foo:

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 - Rails 3 - 测试 Controller 的 GET 方法 - 尝试使用 JSON 并出现 406 错误

我有一个简单的Controller,它指定:respond_to:json当我尝试构建这样调用它的功能测试时:test"GET"doget'index',:format=>:jsonend一切正常。但是,如果我尝试像这样传递查询参数:test"GET"doget'index',{:my_param='1234'},:format=>:jsonend我收到Controller返回的406错误。如果我通过response.inspect转储响应,我可以看到@status=406和@header的内容类型为文本/html。如果我通过response.inspect为不传递查询参数的简单情况转

ruby-on-rails - 将 form_for 标签与 get 方法一起使用

我正在尝试使用get方法提交表单。早些时候我尝试用form_tag做类似的事情并且它工作正常但现在当我更改为form_for标签时,这似乎不起作用。filter_path,:method=>:get)do|f|%>我收到一个无路由错误。 最佳答案 如果需要,您可以使用:html传递原始HTML属性。对于Rails3:filter_path,:html=>{:method=>'GET'})do|f|%>更新并且在Rails4中,根据下面@andre.orvalho的建议,可以直接提供method参数:

ruby - 导轨 : how to get a file extension/postfix based on the mime type

我的问题是,RubyonRails是否具有类似于以下的功能:file_content_type=MIME::Types.type_for(file).first.content_type这将返回特定mime类型的文件扩展名或后缀?所以如果我传入'image/jpeg'函数将返回'jpg'寻找一种比编写完成相同工作的case语句更简洁的编码方式。 最佳答案 Rack::Mime具有这种能力(Rack是Rails的依赖):require'rack/mime'Rack::Mime::MIME_TYPES.invert['image/jpe

ruby - 如何使用修改后的 header 制作 HTTP GET?

使用修改后的header在Ruby中发出HTTPGET请求的最佳方式是什么?我想从日志文件的末尾获取一系列字节,并一直在玩弄以下代码,但服务器返回一个响应说“这是服务器无法理解的请求”(服务器是Apache)。require'net/http'require'uri'#with@address,@port,@pathalldefinedelsewherehttpcall=Net::HTTP.new(@address,@port)headers={'Range'=>'bytes=1000-'}resp,data=httpcall.get2(@path,headers)有没有更好的方法在R

ruby - 为什么 rubocop 或 ruby​​ 风格指南不喜欢使用 get_ 或 set_?

我在我的项目上运行rubocop并修复它提出的投诉。一个特别的提示困扰着我Donotprefixreadermethodnameswithget_我无法从这个投诉中了解太多,所以我查看了sourcecodeingithub.我找到了这个片段defbad_reader_name?(method_name,args)method_name.start_with?('get_')&&args.to_a.empty?enddefbad_writer_name?(method_name,args)method_name.start_with?('set_')&&args.to_a.one?end

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