Skip to content

Instantly share code, notes, and snippets.

@bryanwoods
Created April 4, 2018 14:03
Show Gist options
  • Save bryanwoods/090250f56e3e5a40620978d0d5445cdc to your computer and use it in GitHub Desktop.
Save bryanwoods/090250f56e3e5a40620978d0d5445cdc to your computer and use it in GitHub Desktop.
Warn if a similar hash key exists to the one requested
class Hash
def [](key)
result = fetch(key, nil) and
return result
keys.map(&:to_s).include?(key.to_s) or
return result
if [String, Symbol].include?(key.class)
send("similar_#{key.class.to_s.downcase}_key_warning", key)
end
result
end
private
def similar_string_key_warning(key)
warn "WARNING: No key \"#{key}\" (String) found in Hash #{self}."
warn "Did you mean :#{key} (Symbol)?"
end
def similar_symbol_key_warning(key)
warn "WARNING: No key :#{key} (Symbol) found in Hash #{self}."
warn "Did you mean \"#{key}\" (String)?"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment