DEV Community

loading...
Cover image for Sharpen your Ruby: Iterators

Sharpen your Ruby: Iterators

ericchapman profile image Eric The Coder ・2 min read

I develop in Javascript, Python, PHP and Ruby. By far Ruby is my favorite programming language.

Together let start a journey and revisit our Ruby foundations.

Each post will include some theory but also exercise and solution.

If you have any questions/comments or your are new and need help, you can comment below or send me a message.

Classic Loop

In Ruby like any other programming language we can iterate a number of fix or variable times. Here are a classic infinite loop that will execute until the program crash.

loop do
  put 'Hello World'
end
Enter fullscreen mode Exit fullscreen mode

While loop

It is possible loop while a condition is meet.

number = 0
while number < 100
  puts number
  number += 1
end
# Will print numbers from 0 to 99
Enter fullscreen mode Exit fullscreen mode

Loop until

It is possible loop until a condition is meet.

number = 0
until number == 10
  puts number
  number += 1
end
# Will print numbers from 0 to 9
Enter fullscreen mode Exit fullscreen mode

Loop x number of times

(1..10).each do |i|
  puts i
end
# print numbers from 1 to 19
Enter fullscreen mode Exit fullscreen mode

or also

10.times { puts "Hello World" }
Enter fullscreen mode Exit fullscreen mode

Loop through each element

By far, the most use loop is the 'each' iteration

# Array of names
names = ['Peter', 'Mike', 'John']

# Iterate the names list
names.each do |name|  
   puts name
end

# or shorthand version
names.each { |name| puts name }
Enter fullscreen mode Exit fullscreen mode

Break and Continue

It is possible to stop the loop before the end. It is also possible to skip one iteration and go to the next one.

names.each do |name|  
  next if name === 'Mike' 
  puts name
end
# Peter 
# John
Enter fullscreen mode Exit fullscreen mode

The iteration will skip the puts statement if the name is equal to Mike

names.each do |name|  
  break if name === 'Mike' 
  puts name
end
# Peter
Enter fullscreen mode Exit fullscreen mode

The iteration will stop if the name is equal to Mike.

Exercise

Create a little program that:

  • Create a loop with 10 iterations (from number 1 to 10)
  • Each iteration will print only if the iteration number is even. Odd number will be skip.

Solution

10.times do |num|
  next if num.odd?
  puts "Number #{num}"
end
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)

If you have any comments or questions please do so here or send me a message on twitter.

I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman

Discussion (1)

Collapse
topofocus profile image
Hartmut B. • Edited

Its interesting though, you titled this part with »Iterators« and skipped the Enumerator-Class at all. Problem is: If you start conventionally, you deal with program structures.
Iterations are a complete different approach.

To illustrate:
Boss asks : get me list of consecutive third_fridays, and quick

you just write one single line

    a=(Date.today ..).lazy.select{|x| x == x.end_of_quarter.third_friday }
    => #<Enumerator::Lazy: #<Enumerator::Lazy: Wed, 03 Jul 2019..>:select> 

    > a.next  => Fri, 20 Sep 2019 
    > a.next   => Fri, 20 Dec 2019 
    > a.next   => Fri, 20 Mar 2020 
Enter fullscreen mode Exit fullscreen mode

This is the power of iterations in ruby!

Forem Open with the Forem app