Class: Hash
Instance Method Summary collapse
-
#delete_keys!(arr = [], opt = {}) ⇒ Object
:nodoc:.
-
#only_keep(arr = [], opt = {}) ⇒ Object
Same as pass_keys!() but replaces the hash with the resulting hash.
-
#pass_keys!(*keys) ⇒ Object
Returns a subset of the hash for the specified keys.
-
#transform_many ⇒ Object
For a hash whose values are arrays, this will set each unique element in a value array as a key and set its values to all the keys it occurred in.
-
#validate_value_klass(klass) ⇒ Object
For a hash whose keys are strings of Class names, this will delete any pairs that have nonexistant class names.
-
#vmap(arg = nil, method = '[]', &block) ⇒ Object
Returns a hash which will set its values by calling each value with the given method and optional argument.
-
#vmap!(*args, &block) ⇒ Object
Same as vmap() but merges its results with the existing hash.
-
#vsize ⇒ Object
For a hash whose values are arrays, this will return a hash with each value substituted by the size of the value.
-
#vsort ⇒ Object
Sorts hash by values, returning them as an array of array pairs.
Instance Method Details
#delete_keys!(arr = [], opt = {}) ⇒ Object
:nodoc:
46 47 48 49 50 51 52 53 |
# File 'lib/core/hash.rb', line 46 def delete_keys!(arr=[],opt={}) #:nodoc: deleted = {} arr.each {|e| puts "deleting #{e}" if opt[:verbose] deleted[e] = self.delete(e) if has_key?(e) } deleted end |
#only_keep(arr = [], opt = {}) ⇒ Object
Same as pass_keys!() but replaces the hash with the resulting hash.
42 43 44 |
# File 'lib/core/hash.rb', line 42 def only_keep(arr=[],opt={}) delete_keys!(self.keys - arr,opt) end |
#pass_keys!(*keys) ⇒ Object
Returns a subset of the hash for the specified keys. These entries will be deleted from the original hash.
57 58 59 |
# File 'lib/core/hash.rb', line 57 def pass_keys!(*keys) delete_keys!(keys) end |
#transform_many ⇒ Object
For a hash whose values are arrays, this will set each unique element in a value array as a key and set its values to all the keys it occurred in. This is useful when modeling one to many relationships with keys and values.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/core/hash.rb', line 65 def transform_many b = {} each {|k,arr| #raise "#{arr.inspect} not an Array" if arr.class != Array arr = [arr] if arr.class != Array arr.each {|e| b.has_key?(e) ? b[e].push(k) : b[e] = [k] } } b end |
#validate_value_klass(klass) ⇒ Object
For a hash whose keys are strings of Class names, this will delete any pairs that have nonexistant class names.
4 5 6 7 8 9 10 11 |
# File 'lib/core/hash.rb', line 4 def validate_value_klass(klass) self.each {|sid,obj| if obj.class != Object.const_get(klass) warn "object of '#{sid}' not a #{klass}" self.delete(sid) end } end |
#vmap(arg = nil, method = '[]', &block) ⇒ Object
Returns a hash which will set its values by calling each value with the given method and optional argument. If a block is passed, each value will be set the value returned by its block call.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/core/hash.rb', line 15 def vmap(arg=nil,method='[]',&block) new = {} if block self.each {|k,v| v1 = yield(k,v) new[k] = v1 } else self.each {|k,v| new[k] = arg.nil? ? v.send(method) : v.send(method,arg) } end new end |
#vmap!(*args, &block) ⇒ Object
Same as vmap() but merges its results with the existing hash.
31 32 33 |
# File 'lib/core/hash.rb', line 31 def vmap!(*args,&block) self.update(vmap(*args,&block)) end |
#vsize ⇒ Object
For a hash whose values are arrays, this will return a hash with each value substituted by the size of the value.
37 38 39 |
# File 'lib/core/hash.rb', line 37 def vsize vmap(nil,'size') end |
#vsort ⇒ Object
Sorts hash by values, returning them as an array of array pairs.
78 79 80 |
# File 'lib/core/hash.rb', line 78 def vsort sort { |a,b| b[1]<=>a[1] } end |