Ruby - how to replace key in a hash by a key from different one?

I have this hash:

CARS = {"Audi"     => 0, 
        "BMW"     => 1,

And this output from ActiveRecord (@top_cars):

{1=>18, 0=>17, 3=>13, 5=>10, 2=>5, 4=>1}

How do I replace the keys from @top_cars by the car names from CARS?

Thank you

EDIT: So the desired output would be like {"BMW"=>18, "Audi"=>17, "Renault"=>13, "Mercedes"=>10, "Ford"=>5, "Porsche"=>1}


This would do the trick: {|key, value| [CARS.key(key), value]}.to_h

possible solution:

@top_cars.inject({}) {|memo, (key,value)| memo.merge(CARS.key(key) => value)}

You could merge cars with itself:

cars = { "Audi"     => 0,
         "Mercedes" => 1,
         "Ford"     => 2,
         "Renault"  => 3,
         "BMW"      => 4,
         "Porsche"  => 5

top_cars = {1=>18, 0=>17, 3=>13, 5=>10, 2=>5, 4=>1}

cars.merge(cars) { |*,n| top_cars[n] }
  #=> {"Audi"=>17, "Mercedes"=>18, "Ford"=>5, "Renault"=>13, "BMW"=>1, "Porsche"=>10} 

This uses the form of Hash#merge where a block is employed to determine the values of keys that are present in both hashes being merged, which here is all the keys.

Need Your Help

Spring Batch partitioning -Queue configuration

spring-batch spring-integration spring-jms jbossmq

We are trying to implement spring batch partitioning.I did an initial study and end up with following questions.

Dynamic Linq SearchFor from entities by Passing Tablename,ColumnName and value as parameter

c# linq entity-framework

I need to write a function called IsExists(string TableName,string KeyColumnName,string ValueToCheck) in DAL which checks whether the data exists in the particular table and in the particular column