Help us understand the problem. What is going on with this article?

スペルミスでエラーが出たら、正しい名前を教えてくれる gem を作った

More than 5 years have passed since last update.

スペルミスでエラーが発生して「おかしい、どこもおかしくない…」みたいなことが良くあるので、正しそうな method や class, attribute があったら教えてくれる gem を作った。

yuki24/did_you_mean

gem 'did_you_mean', group: [:development, :test]

binding_of_caller とか debug_inspector を使っているので、本番環境では絶対に使ってはダメだ。

ローカル変数の名前やメソッド名を間違えた場合

class User
  attr_accessor :first_name, :last_name

  def to_s
    "#{f1rst_name} #{last_name}" # f1rst_name ???
  end
end

user.to_s
# => NameError: undefined local variable or method `f1rst_name' for #<User:0x0000000928fad8>
#
#     Did you mean? #first_name
#

クラス名を間違った場合

class Book
  class TableOfContents
    # ...
  end
end

Book::TableofContents # TableofContents ???
# => NameError: uninitialized constant Book::TableofContents
#
#     Did you mean? Book::TableOfContents
#

public なメソッド名を間違えた場合

# In a Rails controller:
params.with_inddiferent_access
# => NoMethodError: undefined method `with_inddiferent_access' for {}:Hash
#
#     Did you mean? #with_indifferent_access
#

ActiveRecord モデルのカラム名を間違えた場合

User.new(nmee: "wrong flrst name")
# => ActiveRecord::UnknownAttributeError: unknown attribute: nmee
#
#     Did you mean? name: string
#

実際に助かった時の図

did_you_mean.png

あと、URL helper が提供するメソッド(users_path とか)が、複数形なのかどうかよく忘れるので、そういうときも結構助けられます。

ぜひ使ってみて、フィードバック下さい!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away