Iterate
Same as conditionality, Iterate it is also used in programming languages.
Table of contents
- Where Ruby use Iterate?
- Print some data from a container(like Array or Hash).
- Replace some elements in an array.
- Important points
- What’s going to do?
- What are the conditions to stop iterating?
- Ruby methods to iterate.
times
while
each
for
until
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 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 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
break | exit loop |
next | jump 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"]