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
404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/rubyhacks.rb', line 404 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
444 445 446 447 448 449 450 451 452 |
# File 'lib/rubyhacks.rb', line 444 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
431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/rubyhacks.rb', line 431 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
427 428 429 430 |
# File 'lib/rubyhacks.rb', line 427 def random key = random_key return {key => self[key]} end |
#random_key ⇒ Object
423 424 425 |
# File 'lib/rubyhacks.rb', line 423 def random_key keys.random end |
#random_value ⇒ Object
419 420 421 |
# File 'lib/rubyhacks.rb', line 419 def random_value self[random_key] end |