【问题标题】:ruby loop next case exampleruby 循环下一个案例示例
【发布时间】:2010-06-30 17:16:37
【问题描述】:

我正在尝试遍历多个语句,但想遍历每个语句一次,例如:

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 个不同的条件。例如:数组[有 100 个元素] 元素 1 获得条件 1,元素 2 继续条件 2,依此类推。由于有 10 个条件,数组中的第 11 个元素会再次获得条件 1,依此类推(条件 1 条件 2 条件 3 ...)

更新 2:

再次感谢您抽出宝贵时间回复。我很抱歉,我不是很清楚。该数组包含电子邮件。我有 10 台电子邮件服务器,并希望通过每台服务器发送我阵列中的 200 封电子邮件(每台服务器只有 1 封电子邮件)。我希望这是有道理的

【问题讨论】:

  • 对不起,我不明白你在问什么。为什么要循环?
  • 我有一个要循环遍历的数组,但是数组中的每个元素每次都需要经过不同的条件,然后在第一个条件处重新开始。
  • 您应该编辑您的问题以添加有关您实际尝试做的事情的更多详细信息。听起来你可能做错了。

标签: ruby


【解决方案1】:

如果我没看错,您希望通过少量服务器发送大量电子邮件,同时平衡负载。尝试创建一个类来管理服务器(这是基本思想)

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 的类,其中包含您的各个服务器等的信息。

【讨论】:

  • 我的回答是响应 OP 的更新,他将其作为 cmets 发布到 Adrian 的回答中,而不是更新问题。我将编辑问题并将其复制到那里以防止混淆。
  • 这个方案如何平衡负载?
【解决方案2】:
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.

【讨论】:

  • 感谢您的回复。很有帮助!
【解决方案3】:

这有帮助吗?我不知道你想做什么。

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

5.times do |count| 将执行该块五次,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 个不同的条件,例如:数组[有 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_serversemails 并使其仍然有效,即使长度已更改。

【讨论】:

  • 感谢您的回复,我要做的是遍历一个数组,并将数组的每个元素应用于 10 个不同的条件,例如:array[has 100 elements] element 1 gets条件 1 元素 2 继续条件 2,依此类推,因为有 10 个条件,数组中的第 11 个元素将再次获得条件 1,依此类推。条件1 条件2 条件3
  • 再次感谢您抽出宝贵时间回复。我很抱歉,我不是很清楚。该阵列包含电子邮件,我有 10 个电子邮件服务器,并希望通过每个服务器发送我在阵列中的 200 封电子邮件(每个服务器只有 1 封电子邮件)。我希望这是有道理的。
  • 如果最后一次编辑有帮助,您能否单击我的答案顶部附近的复选标记图标?我仍然很乐意提供帮助。谢谢。 :)
  • 感谢您的帮助!这应该够了吧。再次感谢
【解决方案4】:
array.each_slice(10) do |emails|
    servers.zip(emails) { |server,email| server<<email }
end

(红宝石 1.9.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2020-09-12
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多