Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/dicom/extensions/hash.rb
Overview
Extensions to the Hash class used by the dicom gem.
Instance Method Summary collapse
-
#create_key_gap_at(index) ⇒ Hash
Creates a gap in the integer keys at the specified index.
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.
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 |