Project

General

Profile

Actions

Feature #14785

open

Parse guard statements like regular conditionals

Added by jacobevelyn (Jacob Evelyn) about 3 years ago. Updated about 3 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:87242]

Description

I just learned that these two pieces of code are not the same:

def test
  if result = calculate_result
    return result
  end

  ...
end
def test
  return result if result = calculate_result

  ...
end

The second one will give this unhelpful error message:

NameError: undefined local variable or method `result' for main:Object
Did you mean?  result

I think this is confusing, especially since tools like RuboCop sometimes encourage users to change conditionals to inline ones ("guard statements"). I'd like to propose that Ruby be changed to treat these as the same.

Actions

Also available in: Atom PDF