Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/imw/utils/extensions/hash.rb
Direct Known Subclasses
Class Method Summary collapse
-
.zip(keys, values, default = nil, &block) ⇒ Object
Create a hash from an array of keys and corresponding values.
Instance Method Summary collapse
-
#assoc(key) ⇒ Object
This is polymorphic to Array#assoc – that is, it allows you treat a Hash and an array of pairs equivalently using assoc().
-
#compact ⇒ Object
remove all key-value pairs where the value is nil.
-
#compact! ⇒ Object
Replaces the hash with its compacted self.
-
#deep_merge(second) ⇒ Object
Merges self with another hash, recursively.
-
#deep_merge!(second) ⇒ Object
Merges self in-place with another hash, recursively.
-
#dispatch(default = nil, &block) ⇒ Object
Works like
Enumerable::find
but loops over the keys of this Hash instead of of arrays of [key,value] and, in the absence of a matching key, doesn’t call the default argument, merely returns it instead. -
#emit(uri) ⇒ Object
Emit the data from this Hash into the given
uri
. -
#from_pairs ⇒ Object
Turns a collection of pairs into a hash.
-
#keep_merge(second) ⇒ Object
Merge another array with this one, accumulating values that appear in both into arrays.
-
#quote_keys_with(final_string = nil) ⇒ Object
Return the elements of this hash in a pretty-printed string, inserting
final_string
between the last two items. - #rassoc(key) ⇒ Object
-
#reverse_merge(other_hash) ⇒ Object
Stolen from ActiveSupport::CoreExtensions::Hash::ReverseMerge.
-
#reverse_merge!(other_hash) ⇒ Object
Stolen from ActiveSupport::CoreExtensions::Hash::ReverseMerge.
-
#slice(*keys) ⇒ Object
Slice a hash to include only the given keys.
-
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys.
-
#terminals(&block) ⇒ Object
Recurses through the pairs of this Hash collecting all String or Symbol “terminal” nodes.
-
#to_openstruct ⇒ Object
Allows loading ostruct directly from YAML.
Class Method Details
.zip(keys, values, default = nil, &block) ⇒ Object
Create a hash from an array of keys and corresponding values.
23 24 25 26 27 |
# File 'lib/imw/utils/extensions/hash.rb', line 23 def self.zip(keys, values, default=nil, &block) hash = block_given? ? Hash.new(&block) : Hash.new(default) keys.zip(values) { |k,v| hash[k]=v } hash end |
Instance Method Details
#assoc(key) ⇒ Object
This is polymorphic to Array#assoc – that is, it allows you treat a Hash and an array of pairs equivalently using assoc(). We remind you that Array#assoc
"Searches through an array whose elements are also arrays comparing obj
with the first element of each contained array using obj.== . Returns the
first contained array that matches (that is, the first associated array)
or nil if no match is found. See also Array#rassoc."
Note that this returns an /array/ of [key, val] pairs.
134 135 136 |
# File 'lib/imw/utils/extensions/hash.rb', line 134 def assoc(key) self.include?(key) ? [key, self[key]] : nil end |
#compact ⇒ Object
remove all key-value pairs where the value is nil
168 169 170 |
# File 'lib/imw/utils/extensions/hash.rb', line 168 def compact reject{|k,v| v.nil? } end |
#compact! ⇒ Object
Replaces the hash with its compacted self
173 174 175 |
# File 'lib/imw/utils/extensions/hash.rb', line 173 def compact! replace(compact) end |
#deep_merge(second) ⇒ Object
Merges self with another hash, recursively.
first =
:balls=> "monkey",
:data=> {
:name=> {:first=> "Sam", :middle=>"I", :last=>"am"}}
second = {
:data=> {
:name=> {:middle=>["you", "me", "everyone we know"], :last => "are"}},
1 => [1,2,5] }
p first.deep_merge(second) # => {:data=>{:name=>{:last=>“are”, :middle=>[“you”, “me”, “everyone we know”], :first=>“Sam”}}, 1=>[1, 2, 5], :balls=>“monkey”} from snippets.dzone.com/posts/show/4706 From: pastie.textmate.org/pastes/30372, Elliott Hird
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/imw/utils/extensions/hash.rb', line 56 def deep_merge(second) target = dup second.keys.each do |key| if second[key].is_a?(Hash) && self[key].is_a?(Hash) target[key] = target[key].deep_merge(second[key]) else target[key] = second[key] end end target end |
#deep_merge!(second) ⇒ Object
Merges self in-place with another hash, recursively.
first =
:balls=> "monkey",
:data=> {
:name=> {:first=> "Sam", :middle=>"I", :last=>"am"}}
second = {
:data=> {
:name=> {:middle=>["you", "me", "everyone we know"], :last => "are"}},
1 => [1,2,5] }
p first.deep_merge(second) # => {:data=>{:name=>{:last=>“are”, :middle=>[“you”, “me”, “everyone we know”], :first=>“Sam”}}, 1=>[1, 2, 5], :balls=>“monkey”}
From: www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html File lib/cerberus/utils.rb, line 42
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/imw/utils/extensions/hash.rb', line 84 def deep_merge!(second) second.keys.each do |key| if second[key].is_a?(Hash) && self[key].is_a?(Hash) self[key].deep_merge!(second[key]) else self[key] = second[key] end end self end |
#dispatch(default = nil, &block) ⇒ Object
Works like Enumerable::find
but loops over the keys of this Hash instead of of arrays of [key,value] and, in the absence of a matching key, doesn’t call the default argument, merely returns it instead.
181 182 183 184 |
# File 'lib/imw/utils/extensions/hash.rb', line 181 def dispatch(default=nil, &block) match = self.keys.find(nil,&block) match ? self[match] : default end |
#emit(uri) ⇒ Object
Emit the data from this Hash into the given uri
.
203 204 205 |
# File 'lib/imw/utils/extensions/hash.rb', line 203 def emit uri IMW.open!(uri).emit(self) end |
#from_pairs ⇒ Object
Turns a collection of pairs into a hash. The first of each pair make the keys and the second the values. Elements with length longer than two will lose those values.
If there are multiple values of
35 36 37 38 39 |
# File 'lib/imw/utils/extensions/hash.rb', line 35 def from_pairs() hsh = { } self.each{ |k,v| hsh[k] = v } hsh end |
#keep_merge(second) ⇒ Object
Merge another array with this one, accumulating values that appear in both into arrays.
Note: array values will be flatten’ed. Sorry.
first =
:balls=> "monkey",
:data=> {
:name=> {:first=> "Sam", :middle=>"I", :last=>"am"}}
second = {
:data=> {
:name=> {:middle=>["you", "me", "everyone we know"], :last => "are"}},
1 => [1,2,5] }
p first.deep_merge(second) # => {:data=>{:name=>{:last=>“are”, :middle=>[“you”, “me”, “everyone we know”], :first=>“Sam”}}, 1=>[1, 2, 5], :balls=>“monkey”} p first.keep_merge(second) # => {:data=>{:name=>{:last=>[“am”, “are”], :middle=>[“I”, “you”, “me”, “everyone we know”], :first=>“Sam”}}, 1=>[1, 2, 5], :balls=>“monkey”}
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/imw/utils/extensions/hash.rb', line 113 def keep_merge(second) target = dup second.each do |key, val2| if second[key].is_a?(Hash) && self[key].is_a?(Hash) target[key] = target[key].keep_merge(val2) else target[key] = target.include?(key) ? [target[key], val2].flatten.uniq : val2 end end target end |
#quote_keys_with(final_string = nil) ⇒ Object
Return the elements of this hash in a pretty-printed string, inserting final_string
between the last two items.
>> {:one => 1, :two => 2, :three => 3}.quote_keys_with "or"
`one', `two', or `three'
8 9 10 |
# File 'lib/imw/utils/extensions/hash.rb', line 8 def quote_keys_with final_string = nil self.keys.quote_items_with final_string end |
#rassoc(key) ⇒ Object
137 138 139 |
# File 'lib/imw/utils/extensions/hash.rb', line 137 def rassoc(key) self.has_value?(key) ? [key, self[key]] : nil end |
#reverse_merge(other_hash) ⇒ Object
Stolen from ActiveSupport::CoreExtensions::Hash::ReverseMerge.
13 14 15 |
# File 'lib/imw/utils/extensions/hash.rb', line 13 def reverse_merge(other_hash) other_hash.merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
Stolen from ActiveSupport::CoreExtensions::Hash::ReverseMerge.
18 19 20 |
# File 'lib/imw/utils/extensions/hash.rb', line 18 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end |
#slice(*keys) ⇒ Object
Slice a hash to include only the given keys. This is useful for limiting an options hash to valid keys before passing to a method:
def search(criteria = {})
assert_valid_keys(:mass, :velocity, :time)
end
search(.slice(:mass, :velocity, :time))
Returns a new hash with only the given keys.
156 157 158 159 160 |
# File 'lib/imw/utils/extensions/hash.rb', line 156 def slice(*keys) require 'set' allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) reject { |key,| !allowed.include?(key) } end |
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys.
163 164 165 |
# File 'lib/imw/utils/extensions/hash.rb', line 163 def slice!(*keys) replace(slice(*keys)) end |
#terminals(&block) ⇒ Object
Recurses through the pairs of this Hash collecting all String or Symbol “terminal” nodes.
189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/imw/utils/extensions/hash.rb', line 189 def terminals &block terminals = [] each_value do |value| if value.respond_to? :terminals then terminals += value.terminals else terminals << value end end terminals.map! {|terminal| yield terminal } if block terminals end |
#to_openstruct ⇒ Object
Allows loading ostruct directly from YAML
142 143 144 |
# File 'lib/imw/utils/extensions/hash.rb', line 142 def to_openstruct map{ |el| el.to_openstruct } end |