rails .each 无缘无故地跳过第二个元素
Posted
技术标签:
【中文标题】rails .each 无缘无故地跳过第二个元素【英文标题】:rails .each is skipping the second element through for no reason 【发布时间】:2014-12-02 08:09:17 【问题描述】:我有以下代码:
@referred_emails = Array.new
@referred_emails = params.select|k, v| k.start_with?('referred_') .values
puts "referred emails array: " + @referred_emails.inspect.to_s
@referred_emails.each_with_index do |referred_email, index|
puts "index is #index. referred_email is #referred_email"
end
这是服务日志:
15:25:34 web.1 | referred emails array: ["theiremail1", "theiremail2", "theiremail3"]
15:25:34 web.1 | index is 0. referred_email is theiremail1
15:25:35 web.1 | index is 1. referred_email is theiremail3
如您所见,由于某种原因,数组中的第二个元素被跳过了。它应该输出以下内容:
15:25:34 web.1 | referred emails array: ["theiremail1", "theiremail2", "theiremail3"]
15:25:34 web.1 | index is 0. referred_email is theiremail1
15:25:34 web.1 | index is 1. referred_email is theiremail2
15:25:35 web.1 | index is 2. referred_email is theiremail3
但由于某种原因,它一直跳过第二个 1。
参数如下:
15:25:34 web.1 | Parameters: "utf8"=>"✓",
"authenticity_token"=>"f7f0idqcQpXG/XzhQYCaEj3xQHLiQz1Nf1z9gF3CMxM=",
"referrer_email"=>"", "referrer_name"=>"", "referred_0"=>"theiremail1",
"referred_1"=>"theiremail2", "referred_2"=>"theiremail3", "commit"=>"SUBMIT"
为什么会跳过第二个元素?
【问题讨论】:
你能告诉我们确切的代码吗? 什么意思?我做了... 你做的过滤更常见的是select
。
向我们展示params
中传递的内容。你的代码有点乱,但应该可以正常工作。
你确定在@referred_emails
被select
方法分配后没有做任何其他操作吗?
【参考方案1】:
不知道你到底出了什么问题,但至少一切似乎都对我有用:
params = "referrer_email"=>"", "referrer_name"=>"", "referred_0"=>"theiremail1", "referred_1"=>"theiremail2", "referred_2"=>"theiremail3"
params.select|k, v| k.include?('referred_') .values
#=> ["theiremail1", "theiremail2", "theiremail3"]
或:
params.select|k, v| k.start_with?('referred_') .values
#=> ["theiremail1", "theiremail2", "theiremail3"]
或:
params.select|k, v| k.include?('referred_') .
each_with_index|(k, v), i| puts "index is #i. referred_email is #v"
#=> index is 0. referred_email is theiremail1
#=> index is 1. referred_email is theiremail2
#=> index is 2. referred_email is theiremail3
【讨论】:
谢谢,但我在创建正确的数组时没有问题。该数组已正确创建(尽管我确实更喜欢您的代码),但是当我循环遍历它时,循环会跳过第二个。我用你的第二个代码块试过了,还是不行。 好吧,我很确定我没有对我在答案中发布的输出进行硬编码,它是由我的 irb 生成和吐出的。您可以在 irb 或 rails 控制台中尝试相同的操作吗? :) @Philip7899 我相信Surya 的意思是你没有给我们足够的信息来重现这个问题。如果我们不能重现问题,我们就无法修复它(因为从我们的角度来看,首先没有问题需要修复)。 奇怪,我在控制台试了一下,得到:'NoMethodError: undefined method `start_with?'对于 #<0x007fdd395b4140>0x007fdd395b4140> 【参考方案2】:您提供的代码绝对没有问题。如果我将以下输出粘贴到名为 test.rb
的空白文件中:
params =
"utf8"=>"✓",
"authenticity_token"=>"f7f0idqcQpXG/XzhQYCaEj3xQHLiQz1Nf1z9gF3CMxM=",
"referrer_email"=>"",
"referrer_name"=>"",
"referred_0"=>"theiremail1",
"referred_1"=>"theiremail2",
"referred_2"=>"theiremail3",
"commit"=>"SUBMIT"
@referred_emails = Array.new
@referred_emails = params.select|k, v| k.start_with?('referred_') .values
puts "referred emails array: " + @referred_emails.inspect.to_s
@referred_emails.each_with_index do |referred_email, index|
puts "index is #index. referred_email is #referred_email"
end
...然后我运行ruby test.rb
,我在 Ruby 2.0+ 上得到以下输出:
referred emails array: ["theiremail1", "theiremail2", "theiremail3"]
index is 0. referred_email is theiremail1
index is 1. referred_email is theiremail2
index is 2. referred_email is theiremail3
如果您的代码中存在错误,则不在您提供的代码示例中。
【讨论】:
以上是关于rails .each 无缘无故地跳过第二个元素的主要内容,如果未能解决你的问题,请参考以下文章
OracleDataReader reader.Read() 从第二个记录行开始读取行,跳过第一行记录