开发者

ruby loop next case example

开发者 https://www.devze.com 2023-01-05 11:31 出处:网络
I am trying to loop through multiple statements, but want to go through each one once, example: while count < 5 do

I am trying to loop through multiple statements, but want to go through each one once, example:

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

Update 1:

Thanks for the reply, what I'm trying to do is loop through an array and have each element of the array be applied to 10 different conditions. For example: array[has 100 elements] element 1 gets condition 1, element 2 goes on to condition 2, and so on. Since there are 10 conditions, the 11th element in the array would get condition 1 again, and so on (condition 1 condition 2 condition 3 ...)

Update 2:

Thanks again for taking the time to reply. I apologize that I'm not being very clear. The array contains em开发者_Python百科ails. I have 10 email servers and want to send the 200 emails I have in my array through each server (only 1 email per server). I hope that makes sense


If I'm reading you correctly, you want to send a large number of emails through a small number of servers while balancing the load. Try creating a class to manage the servers (here's the basic idea)

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

Now, all you will have to do is call ServerFarm.dispatch_message for an email and it will be sent using one of the available servers. This class assumes that you have a class named Server that holds the info for your individual servers, etc etc.


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

produces

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.


Does this help? I can't tell what you are trying to do.

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

The 5.times do |count| will excecute the block five times with count starting at zero and incrementing each time. %w(one two three four five) is the same as ["one", "two", "three", "four", "five"].

If you want to do five different things consecutively, you do not need a loop. Just put the statements in a row:

# do thing 1
# do thing 2
# do thing 3
# ...

Edit:

"I have an array that I want to loop through, but each element in the array needs to go through a different condition each time and then restart at the first condition."

To loop through an array endlessly, testing each element against conditions:

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

Edit 2:

"Thanks for the reply, what I'm trying to do is loop through an array and have each element of the array be applied to 10 different conditions, example: array[has 100 elements] element 1 gets condition 1 element 2 goes on to condition 2 and so on, since there are 10 conditions the 11th element in the array would get condition 1 again and so on. condition 1 condition 2 condition"

You will need to use the % method on numbers.

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

More information: http://ruby-doc.org/core/classes/Fixnum.html#M001059

Edit three:

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

You can modify email_servers and emails and have it still work, even if the lengths are changed.


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

(Ruby 1.9.2)

0

精彩评论

暂无评论...
验证码 换一张
取 消