Created
April 4, 2018 14:03
-
-
Save bryanwoods/090250f56e3e5a40620978d0d5445cdc to your computer and use it in GitHub Desktop.
Warn if a similar hash key exists to the one requested
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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