[英]Ruby how each loop is executed?
I have issue with technically deep question about loops in ruby. 关于ruby中的循环,我遇到了技术问题。
I have algorithm that is executed sequentially for array of Boolean values and operate on one data structures. 我有一个算法,它按顺序执行布尔值数组并在一个数据结构上运行。
def function(boolean, data_structure)
The key point is that the order of execution is most important thing because expression 关键点在于执行顺序是最重要的因为表达
function(true, data_structure);function(true, data_structure); function(false, data_structure)
will leave other result in data structure than expression 将其他结果留在数据结构而不是表达式
function(true, data_structure);function(false, data_structure); function(true, data_structure)
I spent some time trying with each loop, but I didn't get any problems as other result in data structure due execution similar expression as follow 我花了一些时间尝试每个循环,但我没有得到任何问题,因为执行类似表达式的数据结构的其他结果如下
[true, true, false ....].each do |value| function(value, data_structure) end
My question: in default ruby configuration is my each loop is the same like followed for loop? 我的问题:在默认的ruby配置中我的每个循环都是跟循环一样的吗?
for i in 0..array.size do function(array[i], data_structure) end
Because each loop makes the code much clearer and easier to modify and I was thinking about leave each expression rather than using for loop. 因为每个循环使代码更清晰,更容易修改,我在考虑留下每个表达式而不是使用for循环。 (Of course in my case I have a lot more code rather than calling only function()..)
(当然在我的情况下,我有更多的代码而不是只调用function()..)
Yes, it's identical. 是的,它是完全相同的。 It will loop through the elements of the array, in order.
它将按顺序循环遍历数组的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.