Why use Kernel # loop instead of begin-end-while / until?

2

I'm doing a repeat structure in Ruby using the following structure:

begin
  [código]
end <while/until> [condição]

But RuboCop , which I use as a linting tool, says that I should use Kernel#loop with break . So:

loop do
  [código]
  break <if/unless> [condição]
end

Why? In what cases should begin-end-while/until be used? What are the advantages of Kernel#loop above in relation to the structure I used?

    
asked by anonymous 03.12.2017 / 15:52

1 answer

2

I'm not an expert on Ruby, but from what I've seen it's just a question of legibility.

When you read the code and find a begin think, start that? There is nothing clearly indicating there. Worse, it may be that some case the compiler itself may find that there is the beginning of something earlier and get very wrong.

Using loop makes it explicit that there begins a loop and it is easier to follow the flow and understand what is happening.

I find it interesting to raise the problems, but I do not like the suggestion offered, it does not even have the same semantics as the original code, so it induces error if the person does not understand what they are doing. But unfortunately the language does not offer a better option.

    
03.12.2017 / 18:48