2010-06-30 51 views
1

我通过多条语句试图环,但要通过每一个一次,例如:红宝石环路下一个案例

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封电子邮件)。我希望这是有道理的

+1

对不起,我不明白你是什么要求。为什么要循环? – 2010-06-30 17:20:21

+0

我有一个数组,我想通过循环,但数组中的每个元素需要每次都经历不同的条件,然后在第一个条件重新启动。 – rahrahruby 2010-06-30 17:28:21

+1

您应该编辑您的问题,以添加关于您实际尝试执行的操作的更多详细信息。这听起来像你可能会错误的。 – mckeed 2010-06-30 17:32:27

回答

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类,适用于您的单个服务器的信息,等等等等

+0

。 。等等,什么? – 2010-06-30 18:15:07

+0

我的回答是回应OP的更新,他将这些更新发布为Adrian回答的评论,而不是更新问题。我会编辑问题并将其复制到那里以防止混淆。 – bta 2010-06-30 20:10:20

+0

该解决方案如何平衡负载? – 2010-10-13 09:07:25

0

这是否有帮助?我不知道你在做什么。

5.times do |count| 
    puts 'In condition ' + %w(one two three four five)[count] 
end 

5.times do |count|将excecute块五次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

编辑3

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_serversemails,并有它仍然可以工作,即使长改变。

+0

感谢您的回复,我想要做的是循环通过一个数组,并将数组的每个元素应用于10个不同的条件,例如: 数组[有100个元素] 元素1获取条件1元素2继续条件2等,因为有10个条件,数组中的第11个元素将再次获得条件1,依此类推。 条件1 条件2 条件3 – rahrahruby 2010-06-30 17:36:01

+0

再次感谢您花时间回复。我很抱歉,我不是很清楚。该阵列包含电子邮件,我有10个电子邮件服务器,并希望通过每台服务器发送我在阵列中的200封电子邮件(每台服务器只有1封电子邮件)。我希望这是有道理的。 – rahrahruby 2010-06-30 17:45:55

+0

如果最后一次编辑有帮助,您是否可以单击我答案顶部附近的复选标记图标?我仍然乐意提供帮助。谢谢。 :) – Adrian 2010-06-30 17:54:47

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. 
+0

感谢您的回复。这非常有帮助! – rahrahruby 2010-06-30 18:00:33

0
array.each_slice(10) do |emails| 
    servers.zip(emails) { |server,email| server<<email } 
end 

(红宝石1.9.2)