Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/dicom/extensions/hash.rb

Overview

Extensions to the Hash class used by the dicom gem.

Instance Method Summary collapse

Instance Method Details

#create_key_gap_at(index) ⇒ Hash

Note:

It is assumed that this hash features integers as keys and items as values!

Creates a gap in the integer keys at the specified index. This is achieved by incrementing by one all existing index keys that are equal or bigger than the given index.

Parameters:

  • index (Integer)

    the index at which to clear

Returns:

  • (Hash)

    the modified self



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dicom/extensions/hash.rb', line 13

def create_key_gap_at(index)
  # Extract existing Hash entries to an array:
  pairs = self.sort
  h = Hash.new
  # Change the key of those equal or larger than index and put these key,value pairs back in a new Hash:
  pairs.each do |pair|
    if pair[0] < index
      # The key keeps its old index:
      h[pair[0]] = pair[1]
    else
      # The key (and the value Item) gets its index incremented by one:
      h[pair[0]+1] = pair[1]
      pair[1].index = pair[0]+1
    end
  end
  h
end