UtopiaSourceUtopiaContentSymbolicHash

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 key String | Symbol

The lookup key.

returns Object | Nil

The associated value.

raises KeyError

If key is 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 key String | Symbol

The lookup key.

parameter value Object

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 key String | Symbol

The lookup key.

parameter arguments Array

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 key String | Symbol

The lookup key.

returns Boolean

Whether the symbolic key exists.

Implementation

def include? key
	key = key.to_sym
	
	super
end