From: merch-redmine@... Date: 2020-11-26T19:07:52+00:00 Subject: [ruby-core:101105] [Ruby master Bug#17348] Shadowed method can not be evaluated on the line that it is shadowed Issue #17348 has been updated by jeremyevans0 (Jeremy Evans). Status changed from Open to Rejected This isn't a bug. It's expected that Ruby scans code and as soon as it encounters a local variable definition (the left hand side of `=`) it adds it to the local variable table. Changing this to not set the local variable until after the completion of the assignment expression would break code such as: ```ruby sum = lambda do |x, *v| return x if v.empty? x + sum[*v] end sum[1, 2, 3, 4] # => 10 ``` You can compare to the equivalent that didn't set the local variable until after: ```ruby _sum = lambda do |x, *v| return x if v.empty? x + sum[*v] end sum = _sum sum[1, 2, 3, 4] # NameError ``` ---------------------------------------- Bug #17348: Shadowed method can not be evaluated on the line that it is shadowed https://bugs.ruby-lang.org/issues/17348#change-88780 * Author: d-snp (Tinco Andringa) * Status: Rejected * Priority: Normal * ruby -v: ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- I encountered this in the code a junior Rubyist wrote. Rewriting to more logical code solved our problem, but in my opinion it should not have been an error. I reduced the code to this: ``` ruby def a "" end a = a[0] puts a.inspect ``` Expected: `nil` Got: ``` Traceback (most recent call last): shadowed.rb:4:in `
': undefined method `[]' for nil:NilClass (NoMethodError) ``` Note that: ``` ruby a = "" a = a[0] puts a.inspect ``` Does work as expected, so it is specifically when a method is shadowed that this unexpected behaviour occurs. -- https://bugs.ruby-lang.org/ Unsubscribe: