From: "Earlopain (A S) via ruby-core" Date: 2024-09-17T09:00:06+00:00 Subject: [ruby-core:119234] [Ruby master Feature#20742] Trying to assign to a variable in statement modifier should emit a warning Issue #20742 has been updated by Earlopain (A S). Should this just work? If I do: ```rb if a = 0.zero? p a end ``` it has no problem with it. I would expect these to be equivalent. Never wrote the one-liner myself, the longer form above I definitly do though ---------------------------------------- Feature #20742: Trying to assign to a variable in statement modifier should emit a warning https://bugs.ruby-lang.org/issues/20742#change-109817 * Author: esad (Esad Hajdarevic) * Status: Open ---------------------------------------- There is an example in Control Expressions documentation: ``` p a if a = 0.zero? # raises NameError ���undefined local variable or method ���a������. ``` However, if we had already defined `a` there would be no exception raised. If one uses something like `p` for scratch variable, due to Kernel#p, also no exception is raised. Statement modifier is generally somewhat inverting the code flow (the right part is evaluated first then the left part), so it is not really obvious why binding variables shouldn't follow the same flow. A warning would be very beneficial. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/