Module: Darthjee::CoreExt::Hash::Transposeable
- Included in:
- Darthjee::CoreExt::Hash
- Defined in:
- lib/darthjee/core_ext/hash/transposeable.rb
Overview
Collection of methods for transposing keys and values of hash
Instance Method Summary collapse
-
#transpose ⇒ ::Hash
Transpose matrix swapping keys by values.
-
#transpose! ⇒ ::Hash
Transpose matrix swapping keys by values.
Instance Method Details
#transpose ⇒ ::Hash
Transpose matrix swapping keys by values
45 46 47 48 49 50 51 |
# File 'lib/darthjee/core_ext/hash/transposeable.rb', line 45 def transpose {}.tap do |new_hash| each do |key, value| new_hash[value] = key end end end |
#transpose! ⇒ ::Hash
Transpose matrix swapping keys by values
25 26 27 28 29 |
# File 'lib/darthjee/core_ext/hash/transposeable.rb', line 25 def transpose! transposed = transpose keys.each(&method(:delete)) merge!(transposed) end |