草庐IT

get_items

全部标签

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 'gets'

使用IRB,我想输入一个多行字符串,以便从中删除某些字符。“gets”只允许单行-多行是否有类似的功能。ASCII_project.rb(main):002:0*puts="What'sthetextyouwanttostrip?"=>"What'sthetextyouwanttostrip?"ASCII_project.rb(main):003:0>str=gets我现在想粘贴一段文本-因为新行它不起作用。这就是为什么我要收集多行这是代码#encoding:CP850puts="What'sthetextyouwanttostrip?"str=getsstr.gsub!(/\P{AS

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-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'

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 的 'gets' 包含结束换行符?

我从不需要从gets获得的结尾换行符。有一半时间我忘记了chomp它,这是一种痛苦......它为什么在那里? 最佳答案 像puts(听起来很相似)一样,它被设计用来处理行,使用\n字符。gets接受一个可选参数,用于“拆分”输入(或“只读直到它到达”)。它默认为特殊的全局变量$/,默认情况下包含一个\n。gets是一种非常通用的读取流的方法,并包含此分隔符。如果不这样做,部分流内容将会丢失。 关于ruby-为什么Ruby的'gets'包含结束换行符?,我们在StackOverflow上

ruby-on-rails - rails : How to get has_many associations of a model

如何获取模型的has_many关联?例如,如果我有这个类:classA我想要这样的方法:A.get_has_many返回[B,C]这可能吗?谢谢! 最佳答案 您应该使用ActiveRecordreflections.然后你可以这样输入:A.reflect_on_all_associations.map{|assoc|assoc.name}这将返回你的数组[:B,:C] 关于ruby-on-rails-rails:Howtogethas_manyassociationsofamodel,我