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)


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_boolsObject



444
445
446
447
448
449
450
451
452
# File 'lib/rubyhacks.rb', line 444

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



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

#randomObject



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

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

#random_keyObject



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

def random_key
  keys.random
end

#random_valueObject



419
420
421
# File 'lib/rubyhacks.rb', line 419

def random_value
  self[random_key]
end