class SymbolHash

A subclass of Hash where all keys are converted into Symbols, and optionally, all String values are converted into Symbols.

Public Class Methods

[](*hsh) click to toggle source

@overload [](hash)

Creates a SymbolHash object from an existing Hash

@example
  SymbolHash['x' => 1, :y => 2] # => #<SymbolHash:0x...>
@param [Hash] hash the hash object
@return [SymbolHash] a new SymbolHash from a hash object

@overload [](*list)

Creates a SymbolHash from an even list of keys and values

@example
  SymbolHash[key1, value1, key2, value2, ...]
@param [Array] list an even list of key followed by value
@return [SymbolHash] a new SymbolHash object
# File lib/yard/core_ext/symbol_hash.rb, line 28
def self.[](*hsh)
  obj = new
  if hsh.size == 1 && hsh.first.is_a?(Hash)
    hsh.first.each {|k, v| obj[k] = v }
  else
    0.step(hsh.size, 2) {|n| obj[hsh[n]] = hsh[n + 1] }
  end
  obj
end
new(symbolize_value = true) click to toggle source

Creates a new SymbolHash object

@param [Boolean] symbolize_value converts any String values into Symbols

if this is set to +true+.
# File lib/yard/core_ext/symbol_hash.rb, line 9
def initialize(symbolize_value = true)
  @symbolize_value = symbolize_value
end

Public Instance Methods

[](key) click to toggle source

Accessed a symbolized key @param [#to_sym] key the key to access @return [Object] the value associated with the key

Calls superclass method Hash::[]
# File lib/yard/core_ext/symbol_hash.rb, line 49
def [](key) super(key.to_sym) end
[]=(key, value) click to toggle source

Assigns a value to a symbolized key @param [#to_sym] key the key @param [Object] value the value to be assigned. If this is a String and

values are set to be symbolized, it will be converted into a Symbol.
Calls superclass method
# File lib/yard/core_ext/symbol_hash.rb, line 42
def []=(key, value)
  super(key.to_sym, value.instance_of?(String) && @symbolize_value ? value.to_sym : value)
end
delete(key) click to toggle source

Deleted a key and value associated with it @param [#to_sym] key the key to delete @return [void]

Calls superclass method
# File lib/yard/core_ext/symbol_hash.rb, line 54
def delete(key) super(key.to_sym) end
has_key?(key)
Alias for: key?
key?(key) click to toggle source

Tests if a symbolized key exists @param [#to_sym] key the key to test @return [Boolean] whether the key exists

Calls superclass method
# File lib/yard/core_ext/symbol_hash.rb, line 59
def key?(key) super(key.to_sym) end
Also aliased as: has_key?
merge(hash) click to toggle source

Merges the contents of another hash into a new SymbolHash object

@param [Hash] hash the hash of objects to copy @return [SymbolHash] a new SymbolHash containing the merged data

# File lib/yard/core_ext/symbol_hash.rb, line 74
def merge(hash) dup.merge!(hash) end
merge!(hash)
Alias for: update
update(hash) click to toggle source

Updates the object with the contents of another Hash object. This method modifies the original SymbolHash object

@param [Hash] hash the hash object to copy the values from @return [SymbolHash] self

# File lib/yard/core_ext/symbol_hash.rb, line 67
def update(hash) hash.each {|k, v| self[k] = v }; self end
Also aliased as: merge!