Class: Hash
Direct Known Subclasses
Instance Method Summary collapse
- #+(other) ⇒ Object
- #expand_bools ⇒ Object
- #modify(hash, excludes = []) ⇒ Object (also: #absorb)
- #random ⇒ Object
- #random_key ⇒ Object
- #random_value ⇒ Object
Instance Method Details
#+(other) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/rubyhacks.rb', line 408 def +(other) temp = {} raise TypeError unless other.class == Hash self.each do |key, value| raise "Non unique key set for Hash + Hash" unless other[key].class == NilClass temp[key] = value end other.each do |key, value| raise "Non unique key set for Hash + Hash" unless self[key].class == NilClass temp[key] = value end return temp end |
#expand_bools ⇒ Object
448 449 450 451 452 453 454 455 456 |
# File 'lib/rubyhacks.rb', line 448 def self[:true].each do |key| self[key] = true end self[:false].each do |key| self[key] = false end self end |
#modify(hash, excludes = []) ⇒ Object Also known as: absorb
435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/rubyhacks.rb', line 435 def modify(hash, excludes=[]) hash.each do |key, value| # p key begin self[key] = value.dup unless excludes.include? key rescue TypeError #immediate values cannot be dup'd self[key] = value unless excludes.include? key end end self end |
#random ⇒ Object
431 432 433 434 |
# File 'lib/rubyhacks.rb', line 431 def random key = random_key return {key => self[key]} end |
#random_key ⇒ Object
427 428 429 |
# File 'lib/rubyhacks.rb', line 427 def random_key keys.random end |
#random_value ⇒ Object
423 424 425 |
# File 'lib/rubyhacks.rb', line 423 def random_value self[random_key] end |