Project

General

Profile

Actions

Bug #17348

closed

Shadowed method can not be evaluated on the line that it is shadowed

Added by d-snp (Tinco Andringa) over 3 years ago. Updated over 3 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
[ruby-core:101101]

Description

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:

def a
  ""
end
a = a[0]
puts a.inspect

Expected: nil
Got:

Traceback (most recent call last):
shadowed.rb:4:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

Note that:

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0