Project

General

Profile

Actions

Feature #12262

open

Anti-loop

Added by sawa (Tsuyoshi Sawada) over 8 years ago. Updated over 8 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:74849]

Description

The loop method continues by default, and requires the keyword break to escape. This is good when the continuing cases are the norm and the escaping cases are exceptional:

loop do
  ...
  if ...
    ...
  elsif ...
    ...
  elsif ...
    ...
    break # breaks on exceptional cases
  elsif ...
    ...
  else
    ...
  end
end

But when the continuing cases are exceptional and the escaping cases are the norm, the construction requires a lot of break, and it becomes cumbersome:

loop do
  ...
  if ...
    ...
    break # lot of breaks
  elsif ...
    ...
    break # lot of breaks
  elsif ...
    ...
    break # lot of breaks
  elsif ...
    ...
  else
    ...
    break # lot of breaks
  end
end

I actually see this use case a lot when user input is asked with validation on a command line script.

I request a loop-like method that works in the opposite way to loop, that is, it escapes (i.e., runs only once) by default, and requires a keyword to continue (perhaps next). The second code above would then be written like:

some_loop_like_method do
  ...
  if ...
    ...
  elsif ...
    ...
  elsif ...
    ...
  elsif ...
    ...
    next # continues on exceptional cases
  else
    ...
  end
end
Actions

Also available in: Atom PDF

Like0
Like0Like0