ruby loop refactor
I have a loop that looks like this
def slow_loop(array)
array.each_with_index do |item, i|
next_item = array[i+1]
if next_item && item.attribute == next_item.attribute
do_something_with(next_item)
end
end
end
aside from changing the way do_something_with is called, how can i make this perform better?
thx,
-C
p.s.
Since it appears that this is an 'O(n)' operation, there is apparently no performance to be gained here, so the answer i chose is one that uses a ruby method that already encapsulates this operation. thanks for your help everybody
如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

评论(3)


发布评论
需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。