Why does code below exit the each loop as soon as the if-statement "g2k.has_key?(k)" is false
为什么if语句“g2k.has_key?(k)”为false时,下面的代码会退出每个循环
e.element_children.all? do |n|
k = n.name.to_sym
logger.info("#{n.name} as symbol is '#{k}' is valid? = #{g2k.has_key?(k)}")
if g2k.has_key?(k)
logger.info("#{g2k[k] }= #{n.content}")
# @vehicle_data[g2k[k]] = n.content
end
end
This loops through all element children as intended
这会按预期循环遍历所有元素子元素
e.element_children.all? do |n|
k = n.name.to_sym
logger.info("#{n.name} as symbol is '#{k}' is valid? = #{g2k.has_key?(k)}")
#if g2k.has_key?(k)
# logger.info("#{g2k[k] }= #{n.content}")
# @vehicle_data[g2k[k]] = n.content
#end
end
I'm using rails 3.2 with Ruy 1.9, parsing XML with nokogiri.
我正在使用rails 3.2和Ruy 1.9,用nokogiri解析XML。
2 个解决方案
#1
3
Once all?
finds something false, then it can't be all, so it's going to stop processing.
一切都好吗?发现虚假的东西,然后它不可能全部,所以它将停止处理。
Here's an example: http://rubyfiddle.com/riddles/cb777 --- you'll see that it stops after printing 5 because 5 is not < 5
这是一个例子:http://rubyfiddle.com/riddles/cb777 ---你会看到它在打印5后停止因为5不是<5
(1..20).all? do |i|
puts i
i < 5
end
# prints:
1
2
3
4
5
#2
0
Unless you are capturing the output of the .all? method you should not be using it. .all? is used to make sure every element in an array returns true. .each will just iterate over all of them. .detect will iterate until true is returned.
除非你捕获.all的输出?方法你不应该使用它。 。所有?用于确保数组中的每个元素都返回true。 .each将迭代所有这些。 .detect将迭代,直到返回true。
#1
3
Once all?
finds something false, then it can't be all, so it's going to stop processing.
一切都好吗?发现虚假的东西,然后它不可能全部,所以它将停止处理。
Here's an example: http://rubyfiddle.com/riddles/cb777 --- you'll see that it stops after printing 5 because 5 is not < 5
这是一个例子:http://rubyfiddle.com/riddles/cb777 ---你会看到它在打印5后停止因为5不是<5
(1..20).all? do |i|
puts i
i < 5
end
# prints:
1
2
3
4
5
#2
0
Unless you are capturing the output of the .all? method you should not be using it. .all? is used to make sure every element in an array returns true. .each will just iterate over all of them. .detect will iterate until true is returned.
除非你捕获.all的输出?方法你不应该使用它。 。所有?用于确保数组中的每个元素都返回true。 .each将迭代所有这些。 .detect将迭代,直到返回true。