Sha256: 9881166ff89c6aeb228918bf16918ef00dcbb51ca2d60b92f8368e32a6189d33

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require_relative "exceptions_base"

module Exceptions
  class NoMethodError < ExceptionsBase
    private

    def corrected_cmd
      @corrected_cmd ||= last_cmd.gsub(/\b#{unknown_from_exception}\b/, corrected_word)
    end

    def unknown_from_exception
      exception.to_s.match(exception_regexp)[1]
    end

    def dictionary
      klass_methods = eval(infer_klass).methods # rubocop:disable Security/Eval
      # Early return because built_in class does not have access to `instance_methods`.
      return klass_methods.map(&:to_s) if built_in_klass

      instance_methods = eval(infer_klass).instance_methods(false) # rubocop:disable Security/Eval
      instance_methods.push(klass_methods).flatten.map(&:to_s)
    end

    def exception_regexp
      /`([^']+)' for/
    end

    def klass
      exception_without_class_module = exception.to_s.gsub(":Class", "")
      exception_without_class_module.split.last
    end

    # [].methods and Array.methods have a different output.
    # Array class is a part of the Ruby core library while `[]` is an instance of the Array class.
    # When calling [].methods, we inspect the methods available to instances of the Array class, including those inherited from its class and ancestors.
    def infer_klass
      return klass if klass != "Array"

      "[]"
    end

    def built_in_klass
      infer_klass == "[]"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pry-byetypo-1.3.5 lib/pry-byetypo/exceptions/no_method_error.rb
pry-byetypo-1.3.4 lib/pry-byetypo/exceptions/no_method_error.rb