Class: Hash
Class Method Summary collapse
Instance Method Summary collapse
- #aka(new_key, old_key) ⇒ Object
- #assert_all_pair_presence!(*valid_keys) ⇒ Object
- #assert_all_valid_keys!(*valid_keys) ⇒ Object
- #assert_all_valid_values!(*valid_values) ⇒ Object
- #assert_pair_presence!(*valid_keys) ⇒ Object
- #assert_valid_keys!(*valid_keys) ⇒ Object
- #assert_valid_values!(*valid_values) ⇒ Object
-
#bury(*args) ⇒ Object
rubocop:disable Style/GuardClause.
-
#collate(*others) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#collate!(other_hash) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #collect_keys ⇒ Object
- #collect_values ⇒ Object
- #dearray_singular_values ⇒ Object
- #dearray_singular_values! ⇒ Object
- #dearray_values(idx = 0) ⇒ Object
- #dearray_values!(idx = 0) ⇒ Object
-
#deep_dup ⇒ Object
rubocop:disable Style/CaseEquality.
-
#deep_merge(other_hash, &block) ⇒ Object
rubocop:enable Style/CaseEquality.
- #deep_merge!(other_hash, &block) ⇒ Object
- #delete_unless ⇒ Object
- #delete_values(*values) ⇒ Object
- #demote(key) ⇒ Object
- #demote!(key) ⇒ Object
- #denillify(identity = 0) ⇒ Object
- #denillify!(identity = 0) ⇒ Object
- #diff(hash) ⇒ Object
- #except(*keys) ⇒ Object
- #except!(*keys) ⇒ Object
- #extract!(*keys) ⇒ Object
- #hmap(&block) ⇒ Object
- #hmap! ⇒ Object
- #insert(name, value) ⇒ Object
- #invert ⇒ Object
- #keys?(*check_keys) ⇒ Boolean (also: #has_keys?)
- #nillify ⇒ Object
- #nillify! ⇒ Object
- #only_fill(*keys, placeholder: nil) ⇒ Object
- #only_fill!(*keys, placeholder: nil) ⇒ Object
- #only_keys?(*check_keys) ⇒ Boolean (also: #has_only_keys?)
- #pair?(key, value) ⇒ Boolean
- #promote(key) ⇒ Object
- #promote!(key) ⇒ Object
- #rename_keys(*keys) ⇒ Object
- #rename_keys!(*keys) ⇒ Object
- #reverse_merge(other_hash) ⇒ Object
- #reverse_merge!(other_hash) ⇒ Object
- #sample ⇒ Object
- #sample! ⇒ Object
- #sample_key ⇒ Object
- #sample_key! ⇒ Object
- #sample_value ⇒ Object
- #sample_value! ⇒ Object
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
- #slice!(*keys) ⇒ Object (also: #only!)
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #strip ⇒ Object
- #strip! ⇒ Object
-
#symbolize_and_underscore_keys ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#symbolize_and_underscore_keys! ⇒ Object
rubocop:enable Metrics/MethodLength.
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #to_object ⇒ Object (also: #to_o)
- #update_each ⇒ Object
- #update_keys ⇒ Object
- #update_values ⇒ Object
- #vacant?(key) ⇒ Boolean
Class Method Details
.zip(keys, values) ⇒ Object
8 9 10 |
# File 'lib/lite/ruby/hash.rb', line 8 def zip(keys, values) keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] } end |
Instance Method Details
#aka(new_key, old_key) ⇒ Object
14 15 16 17 |
# File 'lib/lite/ruby/hash.rb', line 14 def aka(new_key, old_key) self[new_key] = self[old_key] if key?(old_key) self end |
#assert_all_pair_presence!(*valid_keys) ⇒ Object
31 32 33 34 35 |
# File 'lib/lite/ruby/hash.rb', line 31 def assert_all_pair_presence!(*valid_keys) return assert_pair_presence!(*valid_keys) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_all_valid_keys!(*valid_keys) ⇒ Object
47 48 49 50 51 |
# File 'lib/lite/ruby/hash.rb', line 47 def assert_all_valid_keys!(*valid_keys) return assert_valid_keys!(*valid_keys) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_all_valid_values!(*valid_values) ⇒ Object
63 64 65 66 67 |
# File 'lib/lite/ruby/hash.rb', line 63 def assert_all_valid_values!(*valid_values) return assert_valid_values!(*valid_values) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_pair_presence!(*valid_keys) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lite/ruby/hash.rb', line 19 def assert_pair_presence!(*valid_keys) each do |key, value| if !valid_keys.include?(key) raise ArgumentError, "Invalid key: #{key.inspect}." \ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}" elsif value.respond_to?(:blank?) ? value.blank? : !value raise ArgumentError, "A #{value.inspect} value for #{key.inspect} is not allowed" end end end |
#assert_valid_keys!(*valid_keys) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/lite/ruby/hash.rb', line 37 def assert_valid_keys!(*valid_keys) each_key do |key| next if valid_keys.include?(key) raise ArgumentError, "Invalid key: #{key.inspect}." \ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}" end end |
#assert_valid_values!(*valid_values) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/lite/ruby/hash.rb', line 53 def assert_valid_values!(*valid_values) each_value do |value| next if valid_values.include?(value) raise ArgumentError, "Invalid value: #{value.inspect}." \ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}" end end |
#bury(*args) ⇒ Object
rubocop:disable Style/GuardClause
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lite/ruby/hash.rb', line 70 def bury(*args) if args.count < 2 raise ArgumentError, '2 or more arguments required' elsif args.count == 2 self[args[0]] = args[1] else arg = args.shift self[arg] = {} unless self[arg] self[arg].bury(*args) unless args.empty? end self end |
#collate(*others) ⇒ Object
rubocop:disable Metrics/MethodLength
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lite/ruby/hash.rb', line 86 def collate(*others) hash = {} each_key { |key| hash[key] = [] } others.each do |other| other.each_key { |key| hash[key] = [] } end each { |key, val| hash[key] << val } others.each do |other| other.each { |key, val| hash[key] << val } end hash.each_value(&:flatten!) hash end |
#collate!(other_hash) ⇒ Object
rubocop:enable Metrics/MethodLength
106 107 108 |
# File 'lib/lite/ruby/hash.rb', line 106 def collate!(other_hash) replace(collate(other_hash)) end |
#collect_keys ⇒ Object
110 111 112 |
# File 'lib/lite/ruby/hash.rb', line 110 def collect_keys collect { |key, _| yield(key) } end |
#collect_values ⇒ Object
114 115 116 |
# File 'lib/lite/ruby/hash.rb', line 114 def collect_values collect { |_, val| yield(val) } end |
#dearray_singular_values ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/lite/ruby/hash.rb', line 131 def dearray_singular_values each_with_object({}) do |(key, val), hash| hash[key] = case val when Array then val.size < 2 ? val[0] : val else val end end end |
#dearray_singular_values! ⇒ Object
140 141 142 |
# File 'lib/lite/ruby/hash.rb', line 140 def dearray_singular_values! replace(dearray_singular_values) end |
#dearray_values(idx = 0) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/lite/ruby/hash.rb', line 118 def dearray_values(idx = 0) each_with_object({}) do |(key, val), hash| hash[key] = case val when Array then val[idx] || val[-1] else val end end end |
#dearray_values!(idx = 0) ⇒ Object
127 128 129 |
# File 'lib/lite/ruby/hash.rb', line 127 def dearray_values!(idx = 0) replace(dearray_values(idx)) end |
#deep_dup ⇒ Object
rubocop:disable Style/CaseEquality
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/lite/ruby/hash.rb', line 145 def deep_dup hash = dup each_pair do |key, value| if key.frozen? && ::String === key hash[key] = value.deep_dup else hash.delete(key) hash[key.deep_dup] = value.deep_dup end end hash end |
#deep_merge(other_hash, &block) ⇒ Object
rubocop:enable Style/CaseEquality
161 162 163 |
# File 'lib/lite/ruby/hash.rb', line 161 def deep_merge(other_hash, &block) dup.deep_merge!(other_hash, &block) end |
#deep_merge!(other_hash, &block) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/lite/ruby/hash.rb', line 165 def deep_merge!(other_hash, &block) merge!(other_hash) do |key, this_val, other_val| if this_val.is_a?(Hash) && other_val.is_a?(Hash) this_val.deep_merge(other_val, &block) elsif block_given? yield(key, this_val, other_val) else other_val end end end |
#delete_unless ⇒ Object
177 178 179 |
# File 'lib/lite/ruby/hash.rb', line 177 def delete_unless delete_if { |key, val| !yield(key, val) } end |
#delete_values(*values) ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/lite/ruby/hash.rb', line 181 def delete_values(*values) each_key.with_object([]) do |key, array| next unless values.include?(self[key]) array << key delete(key) end end |
#demote(key) ⇒ Object
190 191 192 |
# File 'lib/lite/ruby/hash.rb', line 190 def demote(key) dup.demote!(key) end |
#demote!(key) ⇒ Object
194 195 196 197 198 199 |
# File 'lib/lite/ruby/hash.rb', line 194 def demote!(key) return self unless key?(key) self[key] = delete(key) self end |
#denillify(identity = 0) ⇒ Object
201 202 203 |
# File 'lib/lite/ruby/hash.rb', line 201 def denillify(identity = 0) dup.denillify!(identity) end |
#denillify!(identity = 0) ⇒ Object
205 206 207 |
# File 'lib/lite/ruby/hash.rb', line 205 def denillify!(identity = 0) each { |key, val| self[key] = val || identity } end |
#diff(hash) ⇒ Object
209 210 211 212 213 |
# File 'lib/lite/ruby/hash.rb', line 209 def diff(hash) h1 = dup.delete_if { |k, v| hash[k] == v } h2 = hash.dup.delete_if { |k, _| key?(k) } h1.merge(h2) end |
#except(*keys) ⇒ Object
215 216 217 |
# File 'lib/lite/ruby/hash.rb', line 215 def except(*keys) dup.except!(*keys) end |
#except!(*keys) ⇒ Object
219 220 221 |
# File 'lib/lite/ruby/hash.rb', line 219 def except!(*keys) keys.each_with_object(self) { |key, _| delete(key) } end |
#extract!(*keys) ⇒ Object
223 224 225 |
# File 'lib/lite/ruby/hash.rb', line 223 def extract!(*keys) keys.each_with_object(self) { |key, hash| hash[key] = delete(key) if key?(key) } end |
#hmap(&block) ⇒ Object
227 228 229 |
# File 'lib/lite/ruby/hash.rb', line 227 def hmap(&block) dup.hmap!(&block) end |
#hmap! ⇒ Object
231 232 233 |
# File 'lib/lite/ruby/hash.rb', line 231 def hmap! inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) } end |
#insert(name, value) ⇒ Object
235 236 237 238 239 240 |
# File 'lib/lite/ruby/hash.rb', line 235 def insert(name, value) return false if key?(name) store(name, value) true end |
#invert ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/lite/ruby/hash.rb', line 242 def invert each_pair.with_object({}) do |(key, val), hash| if val.is_a?(Array) val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) } else hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key) end end end |
#keys?(*check_keys) ⇒ Boolean Also known as: has_keys?
252 253 254 255 |
# File 'lib/lite/ruby/hash.rb', line 252 def keys?(*check_keys) unknown_keys = check_keys - keys unknown_keys.empty? end |
#nillify ⇒ Object
259 260 261 |
# File 'lib/lite/ruby/hash.rb', line 259 def nillify dup.nillify! end |
#nillify! ⇒ Object
263 264 265 266 267 |
# File 'lib/lite/ruby/hash.rb', line 263 def nillify! each do |key, val| self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) end end |
#only_fill(*keys, placeholder: nil) ⇒ Object
269 270 271 |
# File 'lib/lite/ruby/hash.rb', line 269 def only_fill(*keys, placeholder: nil) keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder } end |
#only_fill!(*keys, placeholder: nil) ⇒ Object
273 274 275 |
# File 'lib/lite/ruby/hash.rb', line 273 def only_fill!(*keys, placeholder: nil) replace(only_fill(*keys, placeholder: placeholder)) end |
#only_keys?(*check_keys) ⇒ Boolean Also known as: has_only_keys?
277 278 279 280 |
# File 'lib/lite/ruby/hash.rb', line 277 def only_keys?(*check_keys) unknown_keys = keys - check_keys unknown_keys.empty? end |
#pair?(key, value) ⇒ Boolean
284 285 286 |
# File 'lib/lite/ruby/hash.rb', line 284 def pair?(key, value) self[key] == value end |
#promote(key) ⇒ Object
288 289 290 |
# File 'lib/lite/ruby/hash.rb', line 288 def promote(key) dup.promote!(key) end |
#promote!(key) ⇒ Object
292 293 294 295 296 |
# File 'lib/lite/ruby/hash.rb', line 292 def promote!(key) return self unless key?(key) { key => delete(key) }.merge(self) end |
#rename_keys(*keys) ⇒ Object
298 299 300 |
# File 'lib/lite/ruby/hash.rb', line 298 def rename_keys(*keys) dup.rename_keys!(*keys) end |
#rename_keys!(*keys) ⇒ Object
302 303 304 305 |
# File 'lib/lite/ruby/hash.rb', line 302 def rename_keys!(*keys) keys = Hash[*keys] keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] } end |
#reverse_merge(other_hash) ⇒ Object
307 308 309 |
# File 'lib/lite/ruby/hash.rb', line 307 def reverse_merge(other_hash) other_hash.merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
311 312 313 |
# File 'lib/lite/ruby/hash.rb', line 311 def reverse_merge!(other_hash) other_hash.merge!(self) end |
#sample ⇒ Object
315 316 317 318 |
# File 'lib/lite/ruby/hash.rb', line 315 def sample key = sample_key [key, fetch(key)] end |
#sample! ⇒ Object
320 321 322 323 324 |
# File 'lib/lite/ruby/hash.rb', line 320 def sample! key, value = sample delete(key) [key, value] end |
#sample_key ⇒ Object
326 327 328 329 |
# File 'lib/lite/ruby/hash.rb', line 326 def sample_key hash_keys = keys hash_keys.at(Random.rand(hash_keys.size - 1)) end |
#sample_key! ⇒ Object
331 332 333 334 335 |
# File 'lib/lite/ruby/hash.rb', line 331 def sample_key! key, = sample delete(key) key end |
#sample_value ⇒ Object
337 338 339 |
# File 'lib/lite/ruby/hash.rb', line 337 def sample_value fetch(sample_key) end |
#sample_value! ⇒ Object
341 342 343 344 345 |
# File 'lib/lite/ruby/hash.rb', line 341 def sample_value! key, value = sample delete(key) value end |
#shuffle ⇒ Object
347 348 349 |
# File 'lib/lite/ruby/hash.rb', line 347 def shuffle Hash[to_a.sample(size)] end |
#shuffle! ⇒ Object
351 352 353 |
# File 'lib/lite/ruby/hash.rb', line 351 def shuffle! replace(shuffle) end |
#slice!(*keys) ⇒ Object Also known as: only!
355 356 357 358 359 360 361 362 |
# File 'lib/lite/ruby/hash.rb', line 355 def slice!(*keys) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end |
#stringify_keys ⇒ Object
367 368 369 |
# File 'lib/lite/ruby/hash.rb', line 367 def stringify_keys each_with_object({}) { |(key, val), hash| hash[key.to_s] = val } end |
#stringify_keys! ⇒ Object
371 372 373 |
# File 'lib/lite/ruby/hash.rb', line 371 def stringify_keys! replace(stringify_keys) end |
#strip ⇒ Object
375 376 377 |
# File 'lib/lite/ruby/hash.rb', line 375 def strip select { |_, val| !val.blank? } end |
#strip! ⇒ Object
379 380 381 |
# File 'lib/lite/ruby/hash.rb', line 379 def strip! reject! { |_, val| val.blank? } end |
#symbolize_and_underscore_keys ⇒ Object
rubocop:disable Metrics/MethodLength
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/lite/ruby/hash.rb', line 400 def symbolize_and_underscore_keys each_with_object({}) do |(key, val), hash| new_key = begin key.to_s .gsub(/::/, '/') .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr(' -', '_') .downcase .to_sym rescue StandardError key end hash[new_key] = val end end |
#symbolize_and_underscore_keys! ⇒ Object
rubocop:enable Metrics/MethodLength
419 420 421 |
# File 'lib/lite/ruby/hash.rb', line 419 def symbolize_and_underscore_keys! replace(symbolize_and_underscore_keys) end |
#symbolize_keys ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/lite/ruby/hash.rb', line 383 def symbolize_keys each_with_object({}) do |(key, val), hash| new_key = begin key.to_s.to_sym rescue StandardError key end hash[new_key] = val end end |
#symbolize_keys! ⇒ Object
395 396 397 |
# File 'lib/lite/ruby/hash.rb', line 395 def symbolize_keys! replace(symbolize_keys) end |
#to_object ⇒ Object Also known as: to_o
423 424 425 |
# File 'lib/lite/ruby/hash.rb', line 423 def to_object JSON.parse(to_json, object_class: OpenStruct) end |
#update_each ⇒ Object
429 430 431 |
# File 'lib/lite/ruby/hash.rb', line 429 def update_each replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) }) end |
#update_keys ⇒ Object
433 434 435 436 437 |
# File 'lib/lite/ruby/hash.rb', line 433 def update_keys return to_enum(:update_keys) unless block_given? replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val }) end |
#update_values ⇒ Object
439 440 441 442 443 |
# File 'lib/lite/ruby/hash.rb', line 439 def update_values return to_enum(:update_values) unless block_given? replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) }) end |
#vacant?(key) ⇒ Boolean
445 446 447 448 |
# File 'lib/lite/ruby/hash.rb', line 445 def vacant?(key) value = self[key] respond_to?(:blank?) ? value.blank? : !value end |