Project

General

Profile

Actions

Bug #16599

closed

did_you_mean is not activated for NameError and KeyError

Added by sawa (Tsuyoshi Sawada) about 4 years ago. Updated about 4 years ago.

Status:
Closed
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) about 4 years ago

  • Assignee set to yuki24 (Yuki Nishijima)

Updated by zverok (Victor Shepelev) about 4 years 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 4 years 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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0