Module: GuessMethod::GuessConstant
- Included in:
- Module
- Defined in:
- lib/guessmethod.rb
Overview
The GuessConstant module aliases out const_missing and replaces it with its own, which attempts to find a similarly named constant for the object at hand. If it finds one, it returns that instead, and prints a message to stderr explaining as much. If it doesn’t find a suitable constant, the orignial const_missing gets called.
Class Method Summary collapse
-
.extended(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
Class Method Details
.extended(base) ⇒ Object
:nodoc:
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/guessmethod.rb', line 65 def self.extended(base) #:nodoc: base.class_eval do unless respond_to? :const_missing def self.const_missing(sym); super; end end class << self alias_method :unguessed_const_missing, :const_missing alias_method :const_missing, :guess_const_missing end end end |
Instance Method Details
#guess_const_missing(sym) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/guessmethod.rb', line 77 def guess_const_missing(sym) if GuessMethodOptions[:active] begin return unguessed_const_missing(sym) rescue NameError => e possible_consts = GuessMethodGuesser.find_closest(self.constants, sym) case possible_consts.size when 1 call_const = possible_consts.first $stderr.puts GuessMethodOutputter.replacing_const(sym, call_const, self) self.const_get(call_const) when 0 $stderr.puts GuessMethodOutputter.no_const_in_threshold(sym, self) raise e else $stderr.puts GuessMethodOutputter.ambigious_const(sym, possible_consts, self) raise e end end else unguessed_const_missing(sym) end end |