Feature #12591
closedAllow ruby to either catch misspelled "ailas" statements or, possibly more accurately, be more specific in what it reports as an error to the end-user
Description
Hello ruby-core team.
I lately had a strange error message.
The code was like this (I made a shorter example so that you
can reproduce it):
class Foo
def bar
puts 'This is hello world from method bar().'
end; alias bar1 bar
ailas bar2 bar
end
foo = Foo.new
foo.bar1
foo.bar2
(If you want to copy/paste from another site, here is a link to
gist github with the above code):
https://gist.githubusercontent.com/shevegen/7bfb8cce3f191d5ba7372fc5b23924f7/raw/72057e89ae824c2792328d8ab09c364054104add/wrong_alias.rb
Now, I saved this as the file foo.rb and had ruby evaluate/execute
this file, and the error I got was this one here:
foo.rb:8:in `<class:Foo>': undefined local variable or method `bar' for Foo:Class (NameError)
However had, as you can see above, the name "ailas" was a typo.
I meant to write "alias". This is now another small typo that
I did; in the past I also wrote "def intialize", thankfully we
now have the did-you-mean-it gem.
So I was trying to see if this gem might help here as well.
The gem author Yuki Nishijima has given an example:
https://github.com/yuki24/did_you_mean#correcting-an-instance-variable-when-its-incorrectly-typed
So let's try this via:
require 'did_you_mean/experimental'
(I think I had to do a "gem install did_you_mean", experimental.rb is
perhaps not part of default ruby. But this is an aside, after the
gem install, the above require statement worked.)
The error still is the same, though:
foo.rb:8:in `<class:Foo>': undefined local variable or method `bar' for Foo:Class (NameError)
As you can see, if you replace "ailas" with "ailas" then the error
will go away. But I was thinking that ruby tells me the wrong error
message here, in this case. bar() actually already exists, but what
does not exist is the word "ailas", only "alias". I assume that it
may not be very trivial to make this available for error reporting
BUT it also is a typo that I have made, and so I thought I would
report it.
I would feel that either ruby by default should catch this error,
or perhaps make a better evaluation of the line (an error message
such as "mispelled alias" would be nice but I also understand
that this feature is possibly unwanted to enable by default,
since in all those cases where no typo is in code, it would be
wasted CPU time) or possibly with the experimental feature of
the did_you_mean gem.
I do not know how simple it would be to implement it or if this
has been reported already, but in the event that it has already
been reported, it may be better to report it more than once rather
than zero times.
Thanks.
PS: I filed this under features, because it probably is not a
bug in the sense that the behaviour would be wrong; I just consider
the error reporting in this context not perfect either, so I
proposed it as a new feature or more accurately a feature change.