This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’].
@param constructor
The default value for the mash. Defaults to an empty hash.
@details [Alternatives]
If constructor is a Hash, a new mash will be created based on the keys of the hash and no default value will be set.
# File lib/extlib/mash.rb, line 13 13: def initialize(constructor = {}) 14: if constructor.is_a?(Hash) 15: super() 16: update(constructor) 17: else 18: super(constructor) 19: end 20: end
@param key
The value to set the key to.
@see Mash#convert_key @see Mash#convert_value
# File lib/extlib/mash.rb, line 44 44: def []=(key, value) 45: regular_writer(convert_key(key), convert_value(value)) 46: end
@param key
@details [Alternatives]
If key is a Symbol and it is a key in the mash, then the default value will be set to the value matching the key.
# File lib/extlib/mash.rb, line 27 27: def default(key = nil) 28: if key.is_a?(Symbol) && include?(key = key.to_s) 29: self[key] 30: else 31: super 32: end 33: end
@param key
The key to delete from the mash.\
# File lib/extlib/mash.rb, line 97 97: def delete(key) 98: super(convert_key(key)) 99: end
@param *rejected
@return [Mash] A new mash without the selected keys.
@example
{ :one => 1, :two => 2, :three => 3 }.except(:one) #=> { "two" => 2, "three" => 3 }
# File lib/extlib/mash.rb, line 108 108: def except(*keys) 109: super(*keys.map {|k| convert_key(k)}) 110: end
@param key
@return [Object] The value at key or the default value.
# File lib/extlib/mash.rb, line 76 76: def fetch(key, *extras) 77: super(convert_key(key), *extras) 78: end
@param key
@return [Boolean] True if the key exists in the mash.
# File lib/extlib/mash.rb, line 63 63: def key?(key) 64: super(convert_key(key)) 65: end
@param hash
@return [Mash] A new mash with the hash values merged in.
# File lib/extlib/mash.rb, line 91 91: def merge(hash) 92: self.dup.update(hash) 93: end
Used to provide the same interface as Hash.
@return [Mash] This mash unchanged.
# File lib/extlib/mash.rb, line 115 115: def stringify_keys!; self end
@return [Hash] The mash as a Hash with symbolized keys.
# File lib/extlib/mash.rb, line 118 118: def symbolize_keys 119: h = Hash.new(default) 120: each { |key, val| h[key.to_sym] = val } 121: h 122: end
@return [Hash] The mash as a Hash with string keys.
# File lib/extlib/mash.rb, line 125 125: def to_hash 126: Hash.new(default).merge(self) 127: end
@param other_hash
A hash to update values in the mash with. The keys and the values will be converted to Mash format.
@return [Mash] The updated mash.
# File lib/extlib/mash.rb, line 53 53: def update(other_hash) 54: other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } 55: self 56: end
@param *indices
The keys to retrieve values for. These will be run through +convert_key+.
@return [Array] The values at each of the provided keys
# File lib/extlib/mash.rb, line 84 84: def values_at(*indices) 85: indices.collect {|key| self[convert_key(key)]} 86: end
@param key
@param [Object]
The converted key. If the key was a symbol, it will be converted to a string.
@api private
# File lib/extlib/mash.rb, line 137 137: def convert_key(key) 138: key.kind_of?(Symbol) ? key.to_s : key 139: end
@param value
@return [Object]
The converted value. A Hash or an Array of hashes, will be converted to their Mash equivalents.
@api private
# File lib/extlib/mash.rb, line 148 148: def convert_value(value) 149: if value.class == Hash 150: value.to_mash 151: elsif value.is_a?(Array) 152: value.collect { |e| convert_value(e) } 153: else 154: value 155: end 156: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.