Issue #14785 has been updated by jacobevelyn (Jacob Evelyn).


Ah you're right Jeremy, I hadn't thought about that case. I still think it's unexpected though that in your example the behavior would change if we split the conditional into an `if...end`, but I don't know if others agree, and I don't know how a proposal like this that could possibly break existing code should move forward.

----------------------------------------
Feature #14785: Parse guard statements like regular conditionals
https://bugs.ruby-lang.org/issues/14785#change-72246

* Author: jacobevelyn (Jacob Evelyn)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I just learned that these two pieces of code are not the same:

~~~ ruby
def test
  if result = calculate_result
    return result
  end

  ...
end
~~~

~~~ ruby
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.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>