草庐IT

get_index

全部标签

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 数组 reverse_each_with_index

我想在数组上使用类似reverse_each_with_index的东西。例子:array.reverse_each_with_indexdo|node,index|putsnodeputsindexend我看到Ruby有each_with_index但它似乎没有相反的。还有其他方法吗? 最佳答案 如果你想要数组中元素的真实索引,你可以这样做['Seriously','Chunky','Bacon'].to_enum.with_index.reverse_eachdo|word,index|puts"index#{index}:#{

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,我

ruby - 在 Ruby 中检测按键(非阻塞)w/o getc/gets

我有一个简单的任务需要等待文件系统上的某些更改(它本质上是一个原型(prototype)编译器)。所以我有一个简单的无限循环,在检查更改的文件后休眠5秒。loopdo#iffileschanged#processfiles#andputsresultsleep5end而不是Ctrl+C敬礼,我宁愿能够测试并查看是否按下了某个键,而不会阻塞循环。本质上,我只需要一种方法来判断是否有传入的按键,然后是一种获取它们直到遇到Q的方法,然后退出程序。我想要的是:defwait_for_Qkey_is_pressed&&get_ch=='Q'endloopdo#iffileschanged#pro

ruby-on-rails - Sprockets::CircularDependencyError in Store#index

我正在遵循手册《使用Rails进行敏捷Web开发》第4版,但我在rails3.1中遇到了sprocketcss的问题。代码css是:http://media.pragprog.com/titles/rails4/code/rails31/depot_e/app/assets/stylesheets/application.css.scss如果我修改app/assets/stylesheets/aplication.css.scss的css代码,我会遇到下一个错误:Sprockets::CircularDependencyErrorinStore#indexShowing/home/ub

ruby-on-rails - rails : Plus sign in GET-Request replaced by space

在Rails3(Ruby1.9.2)中我发送一个请求StartedGET"/controller/action?path=/41_+"但是参数列表是这样的:{"path"=>"/41_","controller"=>"controller","action"=>"action"}这里出了什么问题?-、*或.符号工作正常,只是+将被空格替换。 最佳答案 这是正常的URL编码,theplussignisashorthandforaspace:Withinthequerystring,theplussignisreservedasshor

Ruby Rack - 安装一个默认读取 index.html 的简单 Web 服务器

我正在尝试从本教程中获取一些信息:http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder基本上我想要一个文件config.ru告诉rack读取当前目录,这样我就可以访问所有文件,就像一个简单的apache服务器一样,还可以读取带有索引的默认根目录.html文件...有什么办法吗?我当前的config.ru看起来像这样:runRack::Directory.new('')#thiswouldreadthedirectorybutitdoesn'tsettheroottoindex.htmlmap'/'dofile=File.re

ruby - Ruby 中 const_get 的混淆行为?

根据文档mod.const_get(sym)“返回mod中命名常量的值。”我也知道const_get默认情况下可能会查找接收者的继承链。所以以下工作:classA;HELLO=:hello;endclassB:hello我也知道Ruby中的类是Object的子类,因此您可以使用const_get来查找“全局”常量,即使接收方是一个普通类:classC;endC.const_get(:Array)#=>Array然而,这就是我感到困惑的地方——模块不继承Object。那么,为什么我仍然可以使用const_get从模块中查找“全局”常量?为什么以下方法有效?moduleM;endM.con

ruby-on-rails - 为什么 RSpec 的方法 "get"、 "post"、 "put"、 "delete"在 gem(或 Rails 外部)的 Controller 规范中不起作用?

我不是Rails或Rspec的新手,但我是制作gems的新手。当我测试我的Controller时,REST方法“get”、“post”、“put”、“delete”给我一个未定义的方法错误。您将在下面找到代码,但如果您更喜欢在馅饼中查看它,clickhere.谢谢!这是我的spec_helper:$LOAD_PATH.unshift(File.dirname(__FILE__))$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))require'rubygems'require'active_support'u