When coding in Ruby, we tend to make typo errors. This can take up time if not quickly identified. did_you_mean spellchecker gem helps detect such errors and also presents suggestions that can resolve typo errors.
did_you_mean in Ruby
- did_you_mean gem has made it to Ruby core in Ruby 2.3.
- Thus, Ruby 2.3 and later ships with this gem and it will automatically be required when a Ruby process starts up. No special setup is required.
did_you_mean in Rails
- Previously, Rails had its own logic for SpellChecker. It was based on text gem.
- It used levenshtein distance to figure out suggestion.
- Rails has
did_you_mean gem with command given below.
Incorrect variable correction
- If we use incorrect variable name, let’s say
Let’s say, we have a
class with name
TestClass as given below.
If we spell it incorrectly,
did_you_mean shows suggestion as given below.
Suggests instance / class variable name
Let’s say, we have a instance variable as given below.
If we use the instance variable name incorrectly,
did_you_mean gives use error.
NoMethodError / method suggestion
If we try to access method which does not exist,
did_you_mean gives an error as given below.
KeyError on Hash
Let’s say, we have a hash as given below.
If we access
key which does not exist,
did_you_mean suggests as given below.
It looks up key names from the hash.
did_you_mean also has some experimental features.
Those can be taken a look at
did_you_mean` on Github
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox