- Practice iterating over nested data structures
- Practice building nested data structures
In this lab, you'll be coding your solution in programming_languages.rb
. The reformat_languages
method takes in an argument of languages
and the test suite will call it with an argument of the below hash:
You are given a hash where the keys are the styles of the languages and the values are also hashes. These nested hashes have a key of the name of the language and value of the languages attributes, which is also a hash. These inner-most hashes have key/value pairs of type and type value. Take a look:
languages = {
:oo => {
:ruby => {
:type => "interpreted"
},
:javascript => {
:type => "interpreted"
},
:python => {
:type => "interpreted"
},
:java => {
:type => "compiled"
}
},
:functional => {
:clojure => {
:type => "compiled"
},
:erlang => {
:type => "compiled"
},
:scala => {
:type => "compiled"
},
:javascript => {
:type => "interpreted"
}
}
}
This is the hash you will be operating on and reformatting, programmatically––meaning you will not simply re-write the hash in order to get the tests passing.
Iterate over the hash and build a new hash that has the languages as keys that point to a value of a hash that describes the type and style.
It should match the following format:
{
:ruby => {
:type => "interpreted",
:style => [:oo]
},
:javascript => {
:type => "interpreted",
:style => [:oo, :functional]
},
:python => {
:type => "interpreted",
:style => [:oo]
},
:java => {
:type => "compiled",
:style => [:oo]
},
:clojure => {
:type => "compiled",
:style => [:functional]
},
:erlang => {
:type => "compiled",
:style => [:functional]
},
:scala => {
:type => "compiled",
:style => [:functional]
}
}
- Start by setting a variable, let's call it
new_hash
equal to an empty hash. Throughout your iteration over thelanguages
hash, you can then add new key/value pairs tonew_hash
. At the end of your method, returnnew_hash
. - Remember that you can create key/value pairs by using the
[]=
method on a hash. For example:
hash = {}
hash[:my_key] = "my value"
puts hash
# => {my_key: "my value"}
What if we want to create a key/value pair where the value is yet another hash?
hash = {}
hash[:my_key] = {second_level_key: "second level value"}
puts hash
# =>
{
my_key: {
second_level_key: "second level value"
}
}
- Remember that you can add items to array using the
<<
method. - Remember to use binding.pry to dig into your method and each level of the iteration. Play around with it inside the console. This will help you get the tests passing.