ruby – 跳过Enumerable#each_cons中的’n’次迭代

ruby – 跳过Enumerable#each_cons中的’n’次迭代,第1张

概述是否可以在执行每个块时跳过n次迭代? persons.each_cons(2) do |person| if person[0] == person[1] #SKIP 2 iterations end puts "Howdy? #{person[0]}"end 你不能直接这样做. 您可能想要在阵列上调用uniq,或者如果订单很重要,请查看新的chunk方法: [1,1,1, 是否可以在执行每个块时跳过n次迭代?

persons.each_cons(2) do |person|  if person[0] == person[1]    #SKIP 2 iterations  end  puts "Howdy? #{person[0]}"end
解决方法 你不能直接这样做.

您可能想要在阵列上调用uniq,或者如果订单很重要,请查看新的chunk方法:

[1,1,2,3].uniq # => [1,3][1,3].chunk{|e| e}.map(&:first) # => [1,3]# i.e. two adjacent items will always be different
总结

以上是内存溢出为你收集整理的ruby – 跳过Enumerable#each_cons中的’n’次迭代全部内容,希望文章能够帮你解决ruby – 跳过Enumerable#each_cons中的’n’次迭代所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1272809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存