I apologize in advance for the newbie question!
I have the following nested hash and want to convert it into a nested array, and be able to access individual values using their indices.
vehicles = {
car: { type: 'sedan', color: 'red', year: 2007 },
}
I attempted to do this a couple of different ways below, however, I was not able to access an individual value, like 'sedan' using its index. My questions are 1) How do I access an individual element in each solution? 2) Is it recommended to use .map as I have or to_a for future cases?
car_array1 = vehicles.to_a
# => [[:car, {:type=>"sedan", :color=>"red", :year=>2007}]]
car_array2 = []
vehicles.map { |k, v| car_array2 << [k, v] }
# => [[[:car, {:type=>"sedan", :color=>"red", :year=>2007}]]]
puts 'car_array1:'
p car_array1[0][1]
# {:type=>"sedan", :color=>"red", :year=>2007}
puts '-' * 10
puts 'car_array2'
p car_array2[0][1]
# {:type=>"sedan", :color=>"red", :year=>2007}
vehicles[:car][:color]?