草庐IT

an_array

全部标签

ruby-on-rails - 错误 "' Validate_default_type !': An option' s default must match its type (ArgumentError)"when running Ruby on Rails generate on Windows

我正在关注thistutorial并且刚刚开始。我已经使用geminstallrails安装了RubyonRails,并使用railsnewblog创建了一个博客。教程现在说我需要运行railsgeneratecontrollerWelcomeindex,但是当我这样做时,我得到了这个错误:C:/Ruby22/lib/ruby/gems/2.2.0/gems/thor-0.19.2/lib/thor/parser/option.rb:130:in`validate_default_type!':Anoption'sdefaultmustmatchitstype.(ArgumentErr

Ruby 1.9 Array.to_s 表现不同?

我编写了一个快速的小应用程序,它获取一个包含一些关键字的基本代码文件、一个关键字替换文件,并输出一个替换了关键字的新文件。当我使用Ruby1.8时,我的输出看起来不错。现在,当使用Ruby1.9时,我替换的代码中包含换行符而不是换行符。例如,我看到类似的东西:["\r\nDimRunningNormal_1AsBoolean","\r\nDimRunningNormal_2AsBoolean","\r\nDimRunningNormal_3AsBoolean"]代替:DimRunningNormal_1AsBooleanDimRunningNormal_2AsBooleanDimRun

Ruby - Array.find,但返回 block 的值

我有一个数组,我想要第一个block的结果返回一个真值(又名,不是零)。问题是,在我的实际用例中,测试有一个副作用(我实际上是在一组队列上迭代,然后从顶部弹出),所以我不需要在第一次成功之后评估block。a,b,c=[1,2,3][a,b,c].first_but_value{|i|(i+1)==2}==2a==2b==2c==3有什么想法吗? 最佳答案 break很丑=P如果你想要一个函数式方法,你需要一个惰性map:[nil,1,2,3].lazy.map{|i|i&&i.to_s}.find&:itself#=>"1"如果你

ruby-on-rails - rails : Copying attributes from an object to another using the "attributes" method

让模型Quote具有属性[price,description]让模型Invoice有属性[price,description,priority]让invoice模型Invoice中的对象具有属性{price:10,description:'lamp',priority:10}invoice={price:10,description:'lamp',priority:10}假设我想将invoice属性复制到新的quote。quote=Quote.new(invoice.attributes)这会引发一个错误,即priority在模型Quote中不存在。如何将invoice属性复制到新的q

ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

Ruby array.select 多行 block

我在ruby​​中过滤数组并使用.selectblock来执行此操作。条件非常复杂,单行block是可怕的,但又不是那么大,所以单独的方法似乎有点矫枉过正。因此我想使用多行block。但是我不确定语法。filtered_array=base_array.selectdo|elem|returnfalseifcondition1returnfalseifcondition2returntrueend上面的内容显然是不正确的,因为return退出方法,而不是block,但给出了我正在寻找的内容的想法。我也可以使用多个select语句,但这似乎混淆了我正在尝试做的事情。请注意,上述条件非常复

ruby 摩卡 : is there an equivalent to rspec-mocks' #and_call_original?

Rspec-mocks具有expect(some_object).toreceive(:some_method).and_call_original。我可以用Mocha做这个吗?如果可以,怎么做?some_object.expects(:some_method).......什么? 最佳答案 我相当确定没有办法做到这一点。浏览sourcecode,有评论提到完全替换了原来的方法。#Theoriginalimplementationofthemethodisreplacedduringthetestandthenrestoredatt

ruby-on-rails - ruby rails : How do you check if a file is an image?

如何检查文件是否为图像?我想你可以使用这样的方法:defimage?(file)file.to_s.include?(".gif")orfile.to_s.include?(".png")orfile.to_s.include?(".jpg")end但这可能有点低效而且不正确。有什么想法吗?(我正在使用回形针插件,顺便说一句,但我没有看到任何方法来确定文件是否是回形针中的图像) 最佳答案 请检查一次MIME::Types.type_for('tmp/img1.jpg').first.try(:media_type)=>"image"

arrays - Ruby 数组中的 `return`#map

我有一个方法可以决定在map函数中返回什么。我知道这可以通过分配一个变量来完成,但这就是我认为我可以做到的方式;defsome_method(array)array.mapdo|x|ifx>10returnx+1#orwhateverelsereturnx-1endendend这并不像我预期的那样工作,因为第一次return被命中时,它从方法返回,而不是在map函数中,类似于return在javascript的map函数中的使用方式。有没有办法实现我想要的语法?还是我需要将其分配给一个变量,然后像这样将其卡在末尾:defsome_method(array)array.mapdo|x|r

arrays - 通过 ruby​​ 数组成对迭代

这个问题在这里已经有了答案:Rubyarrayaccess2consecutive(chained)elementsatatime(4个答案)关闭3年前。我如何遍历ruby​​数组并始终获得两个值,当前值和下一个值,例如:[1,2,3,4,5,6].pairwisedo|a,b|#a=1,b=2infirstiteration#a=2,b=3inseconditeration#a=3,b=4inthirditeration#...#a=5,b=6inlastiterationend我的用例:我想测试一个数组是否已排序,通过使用这样的迭代器,我总是可以比较两个值。我没有像在这个问题中那样