Class: Hash

Inherits:
Object show all
Defined in:
lib/rubyhacks.rb

Direct Known Subclasses

FloatHash

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (TypeError)


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_boolsObject



448
449
450
451
452
453
454
455
456
# File 'lib/rubyhacks.rb', line 448

def expand_bools
  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

#randomObject



431
432
433
434
# File 'lib/rubyhacks.rb', line 431

def random
  key = random_key
  return {key =>  self[key]}
end

#random_keyObject



427
428
429
# File 'lib/rubyhacks.rb', line 427

def random_key
  keys.random
end

#random_valueObject



423
424
425
# File 'lib/rubyhacks.rb', line 423

def random_value
  self[random_key]
end