class Hash::NormalizeKeys
¶ ↑
Hash::NormalizeKeys
Constants
- COERCES
a list of methods that need the first param normalized
- VERSION
version
Public Class Methods
new() takes the same params as Hash.new.
# File lib/hash/normalizekeys.rb, line 9 def initialize(*opts, &block) @hsh = Hash.new(*opts, &block) end
Private Class Methods
# File lib/hash/normalizekeys.rb, line 125 def self.normalize_key(key) raise 'override-normalize_key' end
Public Instance Methods
Works like Hash#invert, except the new object is a NormalizeKey object and the keys have been normalized.
# File lib/hash/normalizekeys.rb, line 83 def invert rv = self.class.new self.each do |k, v| rv[v] = k end return rv end
Returns a new NormalizeKeys
object by combining this object and the other object, with keys being normalized.
# File lib/hash/normalizekeys.rb, line 20 def merge(other) rv = self.class.new # initialize with own elements self.each do |k, v| rv[k] = v end # merge in other's elements other.each do |k, v| rv[k] = v end # return return rv end
Merges other into the NormalizeKey object, with other's keys being normalized.
# File lib/hash/normalizekeys.rb, line 38 def merge!(other) # merge in other's elements other.each do |k, v| self[k] = v end # return return self end
Works like Hash#replace, with keys being normalized.
# File lib/hash/normalizekeys.rb, line 52 def replace(other) # reset @hsh @hsh.clear # merge in other's elements other.each do |k, v| self[k] = v end # return return self end
Returns the underlying hash's respond_to? method.
# File lib/hash/normalizekeys.rb, line 14 def respond_to?(m) return @hsh.respond_to?(m) end
Works just like Hash#slice, except the key values are normalized.
# File lib/hash/normalizekeys.rb, line 66 def slice(*org_keys) use_keys = {} org_keys.each do |org_key| use_keys[self.class.normalize_key(org_key)] = nil end return @hsh.slice(*use_keys.keys) end
Returns the results of the underlying hash's to_s
method.
# File lib/hash/normalizekeys.rb, line 77 def to_s return @hsh.to_s end
Works like Hash#values_at, except the key values are normalized.
# File lib/hash/normalizekeys.rb, line 94 def values_at(*keys) rv = [] keys.each do |key| rv.push self[key] end return rv end
Private Instance Methods
All methods not already defined are passed to @hsh. If the method is listed in COERCES
then the first arg is normalized.
# File lib/hash/normalizekeys.rb, line 115 def method_missing(m, *args, &block) if COERCES.include?(m.to_s) and (args.length > 0) args = *args args[0] = self.class.normalize_key(args[0]) end return @hsh.send(m, *args, &block) end