class NameError
  attr_reader :frame_binding

  IGNORED_CALLERS = [
    /( |`)missing_name'/,
    /( |`)safe_constantize'/
  ].freeze
  private_constant :IGNORED_CALLERS

  __to_s__ = instance_method(:to_s)
  define_method(:original_message){ __to_s__.bind(self).call }

  def to_s
    msg = original_message
    msg << did_you_mean?.to_s if IGNORED_CALLERS.all? {|ignored| caller.first(8).grep(ignored).empty? }
    msg
  rescue
    original_message
  end

  def did_you_mean?
    DidYouMean.formatter.new(suggestions).to_s if DidYouMean.enabled? && !suggestions.empty?
  end

  def suggestions
    finder.suggestions
  end

  def finder
    @finder ||= DidYouMean.finders[self.class.to_s].new(self)
  end
end

class NameError
  attr_reader :frame_binding

  IGNORED_CALLERS = [
    /( |`)missing_name'/,
    /( |`)safe_constantize'/
  ].freeze
  private_constant :IGNORED_CALLERS

  __to_s__ = instance_method(:to_s)
  define_method(:original_message){ __to_s__.bind(self).call }

  def to_s
    msg = original_message
    bt  = caller.first(6)

    msg << did_you_mean?.to_s if IGNORED_CALLERS.all? {|ignored| bt.grep(ignored).empty? }
    msg
  rescue
    original_message
  end

  def did_you_mean?
    DidYouMean.formatter.new(suggestions).to_s if DidYouMean.enabled? && !suggestions.empty?
  end

  def suggestions
    finder.suggestions
  end

  def finder
    @finder ||= DidYouMean.finders[self.class.to_s].new(self)
  end
end

Reduce memory allocation by about 40%
Reduce memory allocation by about 40%

https://gist.github.com/yuki24/5025a7b8603c25dcdd89/d3c11b614c06aa0fc1193f252960ecfb8a0c368a
