Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Does this help? I can't tell what you are trying to do.</p> <pre><code>5.times do |count| puts 'In condition ' + %w(one two three four five)[count] end </code></pre> <p>The <code>5.times do |count|</code> will excecute the block five times with <code>count</code> starting at zero and incrementing each time. <code>%w(one two three four five)</code> is the same as <code>["one", "two", "three", "four", "five"]</code>.</p> <p>If you want to do five different things consecutively, you do not need a loop. Just put the statements in a row:</p> <pre><code># do thing 1 # do thing 2 # do thing 3 # ... </code></pre> <p><b>Edit</b>:</p> <p>"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."</p> <p>To loop through an array endlessly, testing each element against conditions:</p> <pre><code>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 </code></pre> <p><b>Edit 2</b>:</p> <p>"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"</p> <p>You will need to use the <code>%</code> method on numbers.</p> <pre><code>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 </code></pre> <p>More information: <a href="http://ruby-doc.org/core/classes/Fixnum.html#M001059" rel="nofollow noreferrer">http://ruby-doc.org/core/classes/Fixnum.html#M001059</a></p> <p><b>Edit three</b>:</p> <pre><code>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 </code></pre> <p>You can modify <code>email_servers</code> and <code>emails</code> and have it still work, even if the lengths are changed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload