LoginSignup
155
153

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-09-26

スペルミスでエラーが発生して「おかしい、どこもおかしくない…」みたいなことが良くあるので、正しそうな 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 とか)が、複数形なのかどうかよく忘れるので、そういうときも結構助けられます。

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

155
153
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
155
153