class SymbolicHash
A hash which forces all keys to be symbols and fails with KeyError when strings are used.
Definitions
def [](key)
Fetch a value using a symbolic key.
Signature
-
parameter
keyString | Symbol The lookup key.
-
returns
Object | Nil The associated value.
-
raises
KeyError If
keyis a string.
Implementation
def [] key
raise KeyError.new("attribute #{key} is a string, prefer a symbol") if key.is_a? String
super key.to_sym
end
def []=(key, value)
Assign a value after coercing its key to a symbol.
Signature
-
parameter
keyString | Symbol The lookup key.
-
parameter
valueObject The value to assign.
-
returns
Object The assigned value.
Implementation
def []= key, value
super key.to_sym, value
end
def fetch(key, *arguments, &block)
Fetch a value after coercing the key to a symbol.
Signature
-
parameter
keyString | Symbol The lookup key.
-
parameter
argumentsArray The arguments.
-
yields
{|key| ...} The missing symbolic key when no default argument is given.
-
returns
Object The associated, default, or block-provided value.
Implementation
def fetch(key, *arguments, &block)
key = key.to_sym
super
end
def include?(key)
Check whether this collection includes the given value.
Signature
-
parameter
keyString | Symbol The lookup key.
-
returns
Boolean Whether the symbolic key exists.
Implementation
def include? key
key = key.to_sym
super
end