Rails uses did_you_mean spell checker ruby gem for suggestions

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 integrated use of did_you_mean gem recently.
Source: did_you_mean

Install did_you_mean gem with command given below.

gem install ruby

Features

Incorrect variable correction
  • If we use incorrect variable name, let’s say methosd.
 methosd
# NameError: undefined local variable or method `methosd' for main:Object
# Did you mean?  methods
#               method
Misspelling correction

Let’s say, we have a class with name TestClass as given below.

class TestClass
end

If we spell it incorrectly, did_you_mean shows suggestion as given below.

Testclasss
NameError: uninitialized constant Testclasss
Did you mean?  TestClass
Suggests instance / class variable name

Let’s say, we have a instance variable as given below.

@site_name = "RubyInRails"

If we use the instance variable name incorrectly, did_you_mean gives use error.

site_name
# NameError: undefined local variable or method `site_name' for main:Object
# Did you mean?  @site_name
NoMethodError / method suggestion
name = 'Akshay'

If we try to access method which does not exist, did_you_mean gives an error as given below.

name.starts_with? 'ak'
# NoMethodError: undefined method `starts_with?' for "Akshay":String
# Did you mean?  start_with?
KeyError on Hash

Let’s say, we have a hash as given below.

hash = {foo: 1, bar: 2, baz: 3}

If we access key which does not exist, did_you_mean suggests as given below. It looks up key names from the hash.

hash.fetch(:fooo)
# => KeyError: key not found: :fooo
#    Did you mean?  :foo

Gem did_you_mean also has some experimental features. Those can be taken a look at did_you_mean` on Github