模組 DidYouMean

DidYouMean 寶石會新增功能,在發生 NameErrorNoMethodError 等錯誤時建議可能的類別/方法名稱。在 Ruby 2.3 或更新版本中,它會在啟動時自動啟用。

@example

methosd
# => NameError: undefined local variable or method `methosd' for main:Object
#   Did you mean?  methods
#                  method

OBject
# => NameError: uninitialized constant OBject
#    Did you mean?  Object

@full_name = "Yuki Nishijima"
first_name, last_name = full_name.split(" ")
# => NameError: undefined local variable or method `full_name' for main:Object
#    Did you mean?  @full_name

@@full_name = "Yuki Nishijima"
@@full_anme
# => NameError: uninitialized class variable @@full_anme in Object
#    Did you mean?  @@full_name

full_name = "Yuki Nishijima"
full_name.starts_with?("Y")
# => NoMethodError: undefined method `starts_with?' for "Yuki Nishijima":String
#    Did you mean?  start_with?

hash = {foo: 1, bar: 2, baz: 3}
hash.fetch(:fooo)
# => KeyError: key not found: :fooo
#    Did you mean?  :foo

停用 did_you_mean

有時,您可能想停用 did_you_mean 寶石,例如除錯錯誤物件本身中的問題。您可以透過指定 --disable-did_you_mean 選項給 ruby 指令來完全停用它

$ ruby --disable-did_you_mean -e "1.zeor?"
-e:1:in `<main>': undefined method `zeor?' for 1:Integer (NameError)

當您無法直接存取 ruby 指令(例如 +rails console+、irb)時,您可以使用 RUBYOPT 環境變數套用選項

$ RUBYOPT='--disable-did_you_mean' irb
irb:0> 1.zeor?
# => NoMethodError (undefined method `zeor?' for 1:Integer)

取得原始錯誤訊息

有時候,您並不想完全停用該寶石,但需要在沒有建議的情況下取得原始錯誤訊息(例如測試)。在這種情況下,您可以在錯誤物件上使用 #original_message 方法

no_method_error = begin
                    1.zeor?
                  rescue NoMethodError => error
                    error
                  end

no_method_error.message
# => NoMethodError (undefined method `zeor?' for 1:Integer)
#    Did you mean?  zero?

no_method_error.original_message
# => NoMethodError (undefined method `zeor?' for 1:Integer)

常數

PlainFormatter

DidYouMean::Formatter 是該寶石的基本預設格式化器。格式化器會回應 message_for 方法,並傳回人類可讀的字串。

SPELL_CHECKERS

待辦事項:在 3.4 開發開始時移除

VERSION
VerboseFormatter

DidYouMean::Formatter 是該寶石的基本預設格式化器。格式化器會回應 message_for 方法,並傳回人類可讀的字串。

公開類別方法

correct_error(error_class, spell_checker) 按一下以切換來源

使用指定的拼字檢查器將 DidYouMean 功能新增到錯誤

# File lib/did_you_mean.rb, line 97
def self.correct_error(error_class, spell_checker)
  if defined?(Ractor)
    new_mapping = { **@spell_checkers, error_class.to_s => spell_checker }
    new_mapping.default = NullChecker

    @spell_checkers = Ractor.make_shareable(new_mapping)
  else
    spell_checkers[error_class.to_s] = spell_checker
  end

  error_class.prepend(Correctable) if error_class.is_a?(Class) && !(error_class < Correctable)
end
formatter() 按一下以切換來源

傳回目前設定的格式化器。預設設定為 DidYouMean::Formatter

# File lib/did_you_mean.rb, line 141
def self.formatter
  if defined?(Ractor)
    Ractor.current[:__did_you_mean_formatter__] || Formatter
  else
    Formatter
  end
end
formatter=(formatter) 按一下以切換來源

更新用於格式化建議的主要格式化器。

# File lib/did_you_mean.rb, line 150
def self.formatter=(formatter)
  if defined?(Ractor)
    Ractor.current[:__did_you_mean_formatter__] = formatter
  end
end
spell_checkers() 按一下以切換來源

傳回錯誤類型和拼字檢查器物件的可共用雜湊映射。

# File lib/did_you_mean.rb, line 92
def self.spell_checkers
  @spell_checkers
end