gpt4 book ai didi

ruby - Ruby 中的循环分配

转载 作者:数据小太阳 更新时间:2023-10-29 06:46:13 26 4
gpt4 key购买 nike

我有 EnquiryConsellor 模型。我想以循环方式将查询分配给辅导员。如果有 3 个顾问和 5 个询问,那么分配应该是:

Enquiry 1 => C1, Enquiry 2 => C2, Enquiry 3 => C3, Enquiry 4 => C1, Enquiry 5 => C2

我可以通过查询数据库并通过缓存进行优化来做到这一点,但正在寻找更好的解决方案。

最佳答案

Array#cycle(无限枚举器)很适合这个:

counselors = %w(C1 C2 C3).cycle
enquiries = Array.new(5){|i| "Enquiry #{(i+1).to_s}"}
enquiries.each{|enq| puts "Do something with #{enq} and #{counselors.next}."}

输出

Do something with Enquiry 1 and C1.
Do something with Enquiry 2 and C2.
Do something with Enquiry 3 and C3.
Do something with Enquiry 4 and C1.
Do something with Enquiry 5 and C2.

关于ruby - Ruby 中的循环分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com