Class: Hash

Inherits:
Object show all
Defined in:
lib/more_ruby/hash.rb

Instance Method Summary collapse

Instance Method Details

#all_keysObject

Gathers all keys in the hash in a flat array, even if it’s multi-level

E.g. from => :b, :c => {:d => :e}, returns [:a, :c, :d]



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/more_ruby/hash.rb', line 24

def all_keys
    list = []
    self.each_pair do |k, v|
        case k
        when Hash
            list += k.all_keys
        when Array
            if k[0].class == Hash # if the array contains hashes
                k.each do |array_item|
                    list += array_item.all_keys
                end
            else
                list << k
            end
        else
            list << k
        end # end case k

        case v
        when Hash
            list += v.all_keys
        when Array
            if v[0].class == Hash # if the array contains hashes
                v.each do |array_item|
                    list += array_item.all_keys
                end
            else
                # do nothing
            end
        else
            # do nothing
        end # end case v
    end
    list
end

#all_valuesObject

Gathers all values in the hash in a flat array, even if it’s multi-level

E.g. from => :b, :c => {:d => :e}, returns [:b, :e]



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/more_ruby/hash.rb', line 62

def all_values
    list = []
    self.each_pair do |k, v|
        case k
        when Hash
            list += k.all_values
        when Array
            if k[0].class == Hash # if the array contains hashes
                k.each do |array_item|
                    list += array_item.all_values
                end
            else
                # do nothing
            end
        else
            # do nothing
        end # end case k

        case v
        when Hash
            list += v.all_values
        when Array
            if v[0].class == Hash # if the array contains hashes
                v.each do |array_item|
                    list += array_item.all_values
                end
            else
                list << v
            end
        else
            list << v
        end # end case v
    end
    list
end

#delete_randomObject



18
19
20
# File 'lib/more_ruby/hash.rb', line 18

def delete_random
    delete(self.random_key)
end

#peachObject

A prettier way of printing a hash Want to p each pair, but have each p appear on a new line in STDOUT



113
114
115
# File 'lib/more_ruby/hash.rb', line 113

def peach
    self.each_pair { |k, v| puts "#{k.inspect} => #{v.inspect}" }
end

#random_keyObject



8
9
10
# File 'lib/more_ruby/hash.rb', line 8

def random_key
    self.keys.random
end

#random_pairObject

Returns a hash of the key and value



13
14
15
16
# File 'lib/more_ruby/hash.rb', line 13

def random_pair
    key = random_key
    {key => self[key]}
end

#random_valueObject



4
5
6
# File 'lib/more_ruby/hash.rb', line 4

def random_value
    self.values.random
end

#remove_empty_fieldsObject

Recursively deletes keys from the hash if the value of that field is nil or empty



118
119
120
121
122
123
124
125
126
127
# File 'lib/more_ruby/hash.rb', line 118

def remove_empty_fields
    self.each_pair do |k, v|
        if self[k].class == Hash
            self[k] = self[k].remove_empty_fields
        else
            self.delete(k) if v.to_s == ""
        end
    end
    self
end

#sort_deepObject

Sorts the hash by its keys. This way, if two versions of the hash (mhich are identical but their k-v pairs are in different order) are both sorted then converted to string (to_s) they will be identical. Assumes that all of the keys in the hash are symbol/string/numeric and are not Hash, Array, etc. Assumes that each key in the hash is unique Sorts deep, not shallow Returns a new hash; does not modify self



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/more_ruby/hash.rb', line 175

def sort_deep
    new = {}
    keys = self.keys.sort
    keys.each do |main_key|
        self.each_pair do |k, v|
            next unless k == main_key
            if v.class == Hash
                new[k] = v.sort_deep
            else
                new[k] = v
            end
        end
    end
    new
end

#stringify_all_values_deepObject

Method to convert all values to strings, for all layers within the hash



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/more_ruby/hash.rb', line 99

def stringify_all_values_deep
    self.each_pair do |k, v|
        case v
        when Array, Hash
            v = v.stringify_all_values_deep
        else
            self[k] = v.to_s
        end
    end
    self
end

#strip_hash_of_keys(array_of_keys_to_strip) ⇒ Object

Method to strip from the hash (and all sub-hashes) all keys matching the entries in the supplied array Entries in the array can be strings or other literals, or regexes Directly deletes from self; returns the modified self



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/more_ruby/hash.rb', line 132

def strip_hash_of_keys(array_of_keys_to_strip)
    self.keys.each do |k|
        strip = false
        array_of_keys_to_strip.each do |i|
            if i.class == Regexp
                strip = true if k =~ i
            else
                strip = true if k == i
            end
        end

        if strip
            self.delete(k)
        else
            if self[k].class == Hash
                self[k] = self[k].strip_hash_of_keys(array_of_keys_to_strip)
            elsif self[k].class == Array
                self[k].each do |h|
                    h = h.strip_hash_of_keys(array_of_keys_to_strip) if h.class == Hash
                end
            end
        end
    end
    self
end

#to_a_deepObject

Method to fully convert the hash into an array, including all sub-hashes to_a only converts the base level



160
161
162
163
164
165
166
167
# File 'lib/more_ruby/hash.rb', line 160

def to_a_deep
    self.keys.each do |k|
        if self[k].class == Hash
            self[k] = self[k].to_a_deep
        end
    end
    self.to_a
end

#to_xmlObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/more_ruby/hash.rb', line 191

def to_xml
    map do |k, v|
        if v.class == Hash
            text = v.to_xml
        else
            text = v
        end
        "<%s>%s</%s>" % [k, text, k]
    end.join
end