我正在尝试循环遍历多个语句,但想要遍历每个语句,例如:
while count < 5 do
count+= (not sure if this how ruby increments counts)
puts "In condition one"
next if count > 1
puts "In condition two"
next if count > 1
#..
end
更新1:
感谢您的回复,我正在尝试做的是循环数组并将数组的每个元素应用于10个不同的条件。例如:array [有100个元素]元素1获取条件1,元素2继续条件2,依此类推。由于有10个条件,数组中的第11个元素将再次获得条件1,依此类推(条件1条件2条件3 ......)
更新2:
再次感谢您抽出宝贵时间回复。我道歉,我不是很清楚。该数组包含电子邮件。我有10个电子邮件服务器,并希望通过每个服务器发送我的数组中的200封电子邮件(每台服务器只有1封电子邮件)。我希望这是有道理的
答案 0 :(得分:0)
这有帮助吗?我不知道你要做什么。
5.times do |count|
puts 'In condition ' + %w(one two three four five)[count]
end
5.times do |count|
将从{0}开始,将count
从零开始并每次递增。 %w(one two three four five)
与["one", "two", "three", "four", "five"]
相同。
如果你想连续做五件事,你不需要循环。只需将这些陈述放在一行:
# do thing 1
# do thing 2
# do thing 3
# ...
修改
“我有一个我想要遍历的数组,但是数组中的每个元素每次都需要经历一个不同的条件,然后在第一个条件下重新启动。”
无休止地遍历数组,根据条件测试每个元素:
arr = ['sdfhaq', 'aieei', 'xzhzdwz']
loop do
arr.each do |x|
case x
when /..h/
puts 'There was a \'h\' at the third character.'
when /.{6}/
puts 'There were at least six characters.'
else
puts 'None of the above.'
end
end
end
编辑2 :
“感谢您的回复,我正在尝试做的是循环数组并将数组的每个元素应用于10个不同的条件,例如:array [有100个元素]元素1获取条件1元素2继续条件2,依此类推,因为有10个条件,数组中的第11个元素将再次获得条件1,依此类推。条件1条件2条件“
您需要在数字上使用%
方法。
arr = Array.new(130) # an array of 130 nil elements.
num_conditions = 10
arr.each_with_index do |x, i|
condition = (i + 1) % num_conditions
puts "Condition number = #{condition}"
end
更多信息:http://ruby-doc.org/core/classes/Fixnum.html#M001059
编辑三:
def send_an_email(email, server)
puts "Sending an email with the text #{email.inspect} to #{server}."
end
email_servers = ['1.1.1.1', '2.2.2.2']
emails = ['How are you doing?', 'When are you coming over?', 'Check out this link!']
emails.each_with_index do |email, i|
send_an_email email, email_servers[i % email_servers.length]
end
您可以修改email_servers
和emails
并使其保持有效,即使长度已更改。
答案 1 :(得分:0)
array = (1..100).to_a
conditions = (1..10).to_a
array.each_with_index do |elem, i|
puts "element %d, using condition %d" % [elem, conditions[i % conditions.length]]
end
产生
element 1, using condition 1
element 2, using condition 2
element 3, using condition 3
element 4, using condition 4
element 5, using condition 5
element 6, using condition 6
element 7, using condition 7
element 8, using condition 8
element 9, using condition 9
element 10, using condition 10
element 11, using condition 1
element 12, using condition 2
etc.
答案 2 :(得分:0)
如果我正确地阅读了您,您希望在平衡负载的同时通过少量服务器发送大量电子邮件。尝试创建一个类来管理服务器(这是基本的想法)
class ServerFarm
def initialize
@servers = []
end
attr_accessor :servers
def add_server(server)
@servers << server
end
def remove_server(x)
if x.is_a?(Numeric) then
@servers.delete_at(x)
elsif x.is_a?(Server)
@servers.delete(x)
end
end
def server_available?
@servers.each {|s| return true if s.available? }
false
end
def dispatch_message(email)
@servers.each_with_index {|s, i|
next unless s.available?
s.dispatch(email)
return i
}
nil
end
end
现在,您所要做的只是致电ServerFarm.dispatch_message
获取电子邮件,然后使用其中一个可用服务器发送。此类假定您有一个名为Server
的类,其中包含各个服务器的信息等。
答案 3 :(得分:0)
array.each_slice(10) do |emails|
servers.zip(emails) { |server,email| server<<email }
end
(Ruby 1.9.2)