Issue #14785 has been updated by shevegen (Robert A. Heiler).


Interesting. I personally very rarely do assignment of variables
followed by if-conditionals in my ruby code.

Perhaps there may be some parsing limitation? Otherwise it does
indeed looks like the same code to me.

As for tools such as rubocop - while this can be ignored for the
suggestion here (since your suggestion is just as valid without
any tools making recommendation), ruby hackers should never 
become autobots working for the tools; it should be the other
way around, including making decisions. But that is just my
personal opinion - rubocop can be customized and adjusted as
far as I know anyway, so it's not a big deal. :)

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

* 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>