0

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}
6
  • 4
    What you want to achieve as a result? Commented Mar 28, 2018 at 17:08
  • I'm curious about how the conversion from a hash to array works. I was expecting an array, such as [:type, 'sedan'], [:color, 'blue'], [:year, 2003]] when using .to_a. I'm confused as to why there are curly braces after the conversion? Commented Mar 28, 2018 at 17:15
  • Ok, that's a simple question and i will answer it right now. If you have other questions, than you should ask it in comments. Commented Mar 28, 2018 at 17:16
  • I don't understand how you have them in the hash to begin with. What happens if you have more than one car? Commented Mar 28, 2018 at 17:30
  • 2
    Why do you need to convert to an array? Do you care that color is the 2nd attribute? Why not just use vehicles[:car][:color]? Commented Mar 28, 2018 at 17:40

2 Answers 2

3

Your question is not clear, but based on your comment, you want something like this:

vehicles[:car].to_a

Which yields this:

[[:type, "sedan"], [:color, "red"], [:year, 2007]]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I’m sorry for not being more clear - I had so many questions in mind and wasn’t quite able to articulate them properly. Thank you for this!
1
vehicles = {
   car: { type: 'sedan', color: 'red', year: 2007 },
 }

Here you have a hash with nested hash in it: vehicles[:car]. When you attempt to convert it to array with .to_a you convert only higher level of this hash but not nested. To achieve what you want you can convert nested hashes also by doing this:

result = vehicles.to_a.map { |v| v.last.to_a }
=> [[[:type, "sedan"], [:color, "red"], [:year, 2007]]]

Either way you are not doing your solution the right way. It seems too strange and complex, but I don't know your case, so ...

4 Comments

or vehicles.values.map(&:to_a).
This makes sense, thank you! I’m not doing anything with the data; I’m simply trying to understand how the conversion works and your explanation made it a bit more clear! One thing, say I leave the nested array as is how would I access an individual value? [[:car, {:type=>"sedan", :color=>"red", :year=>2007}]]
@Ralitza Dont get it fully but maybe something like this? a= [[:car, {:type=>"sedan", :color=>"red", :year=>2007}]]; a.first.last[:type] or a.first.last.to_a
Ah, that's great! Thank you so much for answering all of my questions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.