Project

General

Profile

Bug #16599

did_you_mean is not activated for NameError and KeyError

Added by sawa (Tsuyoshi Sawada) 2 months ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
ruby -v:
2.6, 2.7, perhaps for earlier versions too
[ruby-core:97012]

Description

This document claims that the did_you_mean gem responds to NameError, NoMethodError, and KeyError, but it actually seems to only respond to NoMethodError.

foo()
# >> NoMethodError: undefined method `foo' for main:Object
# >> Did you mean?  for

foo
# >> NameError: undefined local variable or method `foo' for main:Object

Foo
# >> NameError: uninitialized constant Foo

{foo: 1}.fetch(:bar)
# >> KeyError: key not found: :bar

Updated by znz (Kazuhiro NISHIYAMA) 2 months ago

  • Assignee set to yuki24 (Yuki Nishijima)

Updated by zverok (Victor Shepelev) 2 months ago

As far as I can understand, did_you_mean reacts only when it can find contextually appropriate thing with similar name. For me, it is this way:

foo
# NameError (undefined local variable or method `foo' for main:Object) -- did_you_mean can't suggest anything

fox = 1
foo
# NameError (undefined local variable or method `foo' for main:Object)
# Did you mean?  fox

# And similarly:
FOO = 1
Foo
# NameError (uninitialized constant Foo)
# Did you mean?  FOO


# 2.7 only:
{baz: 1}.fetch(:bar)
# KeyError (key not found: :bar)
# Did you mean?  :baz

Updated by jeremyevans0 (Jeremy Evans) about 2 months ago

  • Status changed from Open to Closed

As zverok (Victor Shepelev) 's analysis shows, did_you_mean does activate for NameError and KeyError if it can find suggestions.

As for not suggesting for for foo in NameError but doing so for NoMethodError, I can't tell if that is a bug in did_you_mean. It doesn't seem to be general behavior for keywords, since did_you_mean recommends while for whil in both cases. If you think it is a bug in did_you_mean, you may want to file it upstream: https://github.com/ruby/did_you_mean/issues

Also available in: Atom PDF