草庐IT

find_each

全部标签

ruby - "Could not find a valid gem in any repository"(rubygame 等)

一段时间以来我一直在尝试安装这个“rubygame”gem,但是每当我使用命令时geminstallrubygame会报错:ERROR:Couldnotfindavalidgem'rubygame'(>=0)inanyrepositoryERROR:Whileexecutinggem...(Gem::RemoteFetcher::FetchError)Errno::ETIMEDOUT:Connectiontimedout-connect(2)(http://rubygems.org/latest_specs.4.8.gz)我也尝试过其他gems但结果相似:ERROR:Couldnotf

ruby-on-rails - Rails has_many :through Find by Extra Attributes in Join Model

Ruby和Rails的新手,但我现在已经受过书本教育(这显然没有任何意义,哈哈)。我有两个模型,Event和User通过表EventUser连接classUser:event_usersendclassEventUser:event_usersend这个项目是一个日历,我必须在其中跟踪人们为给定的事件注册和刮掉他们的名字。我认为多对多是一种好方法,但我不能这样做:u=User.find:firstactive_events=u.events.find_by_active(true)因为事件实际上没有额外的数据,所以EventUser模型有。虽然我可以做到:u=User.find:fir

ruby-on-rails - 在 ruby​​ 中告诉 .each 循环的结束

如果我有一个循环,例如users.eachdo|u|#somecodeend其中users是多个用户的散列。什么是最简单的条件逻辑来查看您是否在用户散列中的最后一个用户并且只想为最后一个用户执行特定代码,例如users.eachdo|u|#codeforeveryone#conditionalcodeforlastuser#codeforthelastuserendend 最佳答案 users.each_with_indexdo|u,index|#somecodeifindex==users.size-1#codeforthelas

ruby - Ruby 中 each.with_index 和 each_with_index 的区别?

我真的很困惑each.with_index和each_with_index之间的区别。它们有不同的类型,但在实践中似乎是相同的。 最佳答案 with_index方法采用可选参数来偏移起始索引。each_with_index做同样的事情,但没有可选的起始索引。例如:[:foo,:bar,:baz].each.with_index(2)do|value,index|puts"#{index}:#{value}"end[:foo,:bar,:baz].each_with_indexdo|value,index|puts"#{index}:

ruby each_with_index 偏移量

我可以在each_with_index循环迭代器中定义索引的偏移量吗?我的直接尝试失败了:some_array.each_with_index{|item,index=1|some_func(item,index)}编辑:澄清:我不想要数组偏移我希望each_with_index中的索引不是从0开始,而是例如1. 最佳答案 实际上,Enumerator#with_index接收偏移量作为可选参数:[:foo,:bar,:baz].to_enum.with_index(1).eachdo|elem,i|puts"#{i}:#{elem

ruby - 在 ruby​​ 1.8.6 (each_char) 中遍历字符串的每个字符

我是ruby​​的新手,目前正尝试在ruby​​中独立于基本字符串对每个字符进行操作。我正在使用ruby​​1.8.6并想做类似的事情:"ABCDEFG".each_chardo|i|putsiend这会产生一个未定义的方法“each_char”错误。我期待看到垂直输出:ABCD..etceach_char方法是否仅为1.9定义?我尝试使用普通的each方法,但该block只是在一行中输出整个字符串。我想出如何做到这一点的唯一方法是从头开始创建一个字符数组:['A','B','C','D','...'].eachdo|i|putsiend这会输出所需的内容:ABC..etc是否有一种方

ruby-on-rails - 当没有记录时 find() 为 nil

在我当前的Rails程序中,当我使用类似的东西时user=User.find(10)当没有ID=10的用户时,我会有如下异常:ActiveRecord::RecordNotFound:Couldn'tfindUserwithID=10我可以得到nil而不是引发异常吗,所以当我做类似的事情时:unlessuser=Challenge.find(10)puts"someerrormsg"end我只想在没有记录的时候得到nil,我不想使用begin/rescue谢谢 最佳答案 是的,只是做:Challenge.find_by_id(10)

ruby - Array#each 与 Array#map

hash={"d"=>[11,22],"f"=>[33,44,55]}#case1hash.map{|k,vs|vs.map{|v|"#{k}:#{v}"}}.join(",")=>"d:11,d:22,f:33,f:44,f:55"#case2hash.map{|k,vs|vs.each{|v|"#{k}:#{v}"}}.join(",")=>"11,22,33,44,55"唯一的区别是案例1使用vs.map,案例2使用vs.each。这里发生了什么? 最佳答案 Array#each为数组的每个元素执行给定的block,然后返回数

ruby-on-rails - ActiveRecord.find(array_of_ids),保留顺序

当您在Rails中执行Something.find(array_of_ids)时,结果数组的顺序不取决于array_of_ids的顺序。有什么办法可以找到并保留顺序吗?ATM我根据ID的顺序手动对记录进行排序,但这有点蹩脚。UPD:如果可以使用:order参数和某种SQL子句指定顺序,那么如何? 最佳答案 奇怪的是,没有人提出这样的建议:index=Something.find(array_of_ids).group_by(&:id)array_of_ids.map{|i|index[i].first}除了让SQL后端执行它之外,尽

ruby-on-rails - Rails:Gemfile.lock 需要 "Could not find bundler"(2.2.11)。 ( gem ::GemNotFoundException)

当我尝试执行bundlerupdate时出现此错误:.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in`to_specs':Couldnotfindbundler(>=0)amongst[rake-0.8.7,rake-0.8.7,rubygems-update-1.8.4](Gem::LoadError)我是Ruby的新手,有人能告诉我这是什么原因吗?安装了Rake0.8.7。 最佳答案 如果您安装了bundlerge