Skip to main content Link Search Menu Expand Document (external link)

Iterate

Same as conditionality, Iterate it is also used in programming languages.

Table of contents

  1. times
  2. each
  3. for
  4. while
  5. until
  6. Iterate control
  1. Where Ruby use Iterate?
    1. Print some data from a container(like Array or Hash).
    2. Replace some elements in an array.
  2. Important points
    1. What’s going to do?
    2. What are the conditions to stop iterating?
  3. Ruby methods to iterate.
    1. times
    2. while
    3. each
    4. for
    5. until
    6. loop

times

Ruby can run many times by this METHOD times.

{how many}.times do
   code block
end
irb(main):001:0> 3.times do
irb(main):002:1*   puts 'KodaCamp'
irb(main):003:1> end
KodaCamp
KodaCamp
KodaCamp
=> 3

Ruby also can assign a variable to block.

irb(main):001:0> 3.times do |i|
irb(main):002:1*   puts "KodaCamp #{i} batch"
irb(main):003:1> end
KodaCamp 0 batch 
KodaCamp 1 batch
KodaCamp 2 batch
=> 3

The first index value is 0.

Ruby can use do ~ end replace { ~ }

irb(main):001:0> 3.times { |i| puts "KodaCamp #{i} batch" }
KodaCamp 0 batch 
KodaCamp 1 batch
KodaCamp 2 batch
=> 3

each

Array and Hash have a METHOD each to through all the elements.

# Sum all elements in array
irb(main):001:0> array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> sum = 0
=> 0
irb(main):003:0> array.each do |value|
irb(main):004:1*   sum += value
irb(main):005:1> end
=> [1, 2, 3, 4, 5]
irb(main):006:0> sum
=> 15

# array each modifier
irb(main):001:0> array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> sum = 0
=> 0
irb(main):003:0> array.each { |value| sum += value }
=> [1, 2, 3, 4, 5]
irb(main):006:0> sum
=> 15

sum += value this mean sum = sum + value.
The first sum will be assigned to sum+value.
The second sum is current value.
That means it will assign original value pls value then assigned to sum

for

for is a syntax, different from each or times.

for {variable} in {target}
   code block
end
irb(main):001:0> array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> sum = 0
=> 0
irb(main):003:0> for value in array
irb(main):004:1>   sum += value
irb(main):005:1> end
=> [1, 2, 3, 4, 5]
irb(main):006:0> sum
=> 15

while

The condition which is to be tested, given at the beginning of the loop and all statements are executed until the given boolean condition satisfies. When the condition becomes false, the control will be out from the while loop. It is also known as Entry Controlled Loop because the condition to be tested is present at the beginning of the loop body. So basically, while loop is used when the number of iterations is not fixed in a program.

while logic

while conditional
 # code block
end

irb(main):001:0> sum = 0
=> 0
irb(main):002:0> i = 1
=> 1
irb(main):003:0> while i <= 5
irb(main):004:1>   sum += i
irb(main):005:1>   i += 1
irb(main):006:1> end
=> nil
irb(main):007:0> puts sum
15
=> nil

until

if and unless, while and until is same.

until logic

until conditional
 # code block
end

irb(main):001:0> sum = 0
=> 0
irb(main):002:0> i = 1
=> 1
irb(main):003:0> until i > 5
irb(main):004:1>   sum += i
irb(main):005:1>   i += 1
irb(main):006:1> end
=> nil
irb(main):007:0> puts sum
15
=> nil

Iterate control

breakexit loop
nextjump to the next loop
# break_next.rb
puts 'break example:'
i = 0
["foo", "bar", "baz", "qux", "quux"].each do |lang|
  i += 1
  if i == 3
    break
  end
  p [i, lang]
end
puts 'next example:'
i = 0
["foo", "bar", "baz", "qux", "quux"].each do |lang|
  i += 1
  if i == 3
    next
  end
  p [i, lang]
end
 $~ ruby break_next.rb
break example:
[1, "foo"]
[2, "bar"]
next example:
[1, "foo"]
[2, "bar"]
[4, "qux"]
[5, "quux"]

Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.