Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices in Ruby for loop

Tags:

for-loop

ruby

I came across three ways of writing a loop.

the_count = [1, 2, 3, 4, 5]
  • for loop 1

    for number in the_count
      puts "This is count #{number}"
    end
    
  • for loop 2

    the_count.each do |count|
      puts "The counter is at: #{count}"
    end
    
  • for loop 3

    the_count.each {|i| puts "I got #{i}"}
    

Are there situations in which one way is a good practice or better solution than the other two? The first one is the most similar to the ones in other languages, and for me, the third one looks unorderly.

like image 417
Kevinvhengst Avatar asked Jul 07 '15 08:07

Kevinvhengst


People also ask

How do you use a for loop in Ruby?

The simplest way to create a loop in Ruby is using the loop method. loop takes a block, which is denoted by { ... } or do ... end . A loop will execute any code within the block (again, that's just between the {} or do ...

Does Ruby have for loops?

Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby.

What is the preferred way of iterating through a list of objects in Ruby?

The Ruby Enumerable#each method is the most simplistic and popular way to iterate individual items in an array. It accepts two arguments: the first being an enumerable list, and the second being a block. It takes each element in the provided list and executes the block, taking the current item as a parameter.


1 Answers

The first option is generally discouraged. It is possible in ruby to be more friendly towards developers coming from other languages (as they recognize the syntax) but it behaves a bit strange regarding variable visibility. Generally, you should avoid this variant everywhere and use only one of the block variants.

The advantage of the two other variants is that it works the same for all methods accepting a block, e.g. map, reduce, take_while and others.

The two bottom variants are mostly equivalent You use the each method and provide it with a block. The each method calls the block once for each element in the array.

Which one you use is mostly up to preference. Most people tend to use the one with braces for simple blocks which don't require a line-break. If you want to use a line-break in your block, e.g. if you have multiple statements there, you should use the do...end variant. This makes your code more readable.

There are other slightly more nuanced opinions on when you should use one or the other (e.g. some always use the braces form when writing functional block, i.e. ones which don't affect the outside of the block even when they are longer), but if you follow this above advice, you will please at least 98% of all ruby developers reading your code.

Thus, in conclusion, avoid the for i in ... variants (the same counts for while, until, ...) and always use the block-form. Use the do...end of block for complex blocks and the braces-form for simple one-line blocks.

When you use the the block form, you should however be aware of the slight differences in priority when chaining methods.

This

foo bar { |i| puts i }

is equivalent to

foo(bar{|i| puts i})

while

foo bar do |i|
  puts i
end

is equivalent to

foo(bar) { |i| puts i }

As you can see, in the braces form, the block is passed to the right-most method while in the do...end form, the block is passed to the left-most method. You can always resolve the ambiguity with parenthesis though.

like image 159
Holger Just Avatar answered Sep 30 '22 06:09

Holger Just