Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_sync.rb

Overview

A little enhancement that we need…

Instance Method Summary collapse

Instance Method Details

#transform_keys_by_mapping(mapping) ⇒ Object

Given a mapping (hash), this will transform the keys from the keys to the values in the mapping. For example:

mapping = {:cat => :Cat, :dog => :Canine}
hash = {:cat => 'Felix', :dog => 'Toby'}
hash.transform_keys_by_mapping(mapping)
=> {:Cat => 'Felix', :Canine => 'Toby'}


374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/simple_sync.rb', line 374

def transform_keys_by_mapping(mapping)
  ddup = {}
  self.each do |k,v|
    if mapping[k]
      if mapping[k].is_a?(Proc)
        ddup = mapping[k].call(ddup,v)
      else
        ddup[mapping[k]] = v
      end
    end
  end
  ddup
end