Key and value mapping from one object to another
If there's no "logical" correlation between the key you have, and the key you want (i.e. no consistent way to rewrite them) then you basically have to use a lookup of some sort to do the translation.

So your "reading keys from a hash" strategy is your best bet, but it can be improved a little.

You can be more functional and just use map:

lookup = { data1: :LINE1, data2: :LINE2, data3: :LINE3 }

result = Hash[ lookup.map { |in, out| [out, have[in]] } ]


Or you can just go through the ones in the input hash, and translate the ones that are actually there, discarding ones without a translation:

result = Hash[ have.map { |k, v| [lookup[k], v] if lookup[k] }.compact ]


And for either of those, if you ever need to translate the other way, you can just use lookup.invert to flip the keys/values around.

Lastly, for you spec, don't put an expectation in a after(:each) block. Put the expectation in the spec itself.