Module: BracketErrorSuggestion::NilExt

Defined in:
lib/bracket_error_suggestion/nil_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



7
8
9
10
11
12
# File 'lib/bracket_error_suggestion/nil_ext.rb', line 7

def extended(obj)
  obj.instance_eval do
    alias :method_missing_without_be_suggestion :method_missing
    alias :method_missing :method_missing_with_be_suggestion
  end
end

Instance Method Details

#method_missing_with_be_suggestion(name, *args, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/bracket_error_suggestion/nil_ext.rb', line 15

def method_missing_with_be_suggestion(name, *args, &block)
  return method_missing_without_be_suggestion(name, *args, &block) unless BracketErrorSuggestion.enabled
  case name
  when :[] then
    key = args.first
    raise NoMethodError, "undefined method `[]' for nil:NilClass, maybe it attempted to access: " +
      BracketErrorSuggestion.nil_parent_paths.reverse.map{|path| "#{path}[#{key.inspect}] but #{path} is nil"}.join(", ")
  end
  return method_missing_without_be_suggestion(name, *args, &block)
end