4. Prompting Redux - Don't really understand the add code


#1

Hi,

Okay so I've passed this exercise, however I got the "add to hash" code from the first exercise and this forum (see A below for the code).
I initially tried a line of code we was taught a few exercises before (see B), which according to the exercise for that code, it would get only the letter "s" from another array and put it in the array "symbols". This didn't work for me when I try to adapt it to this exercise.

So I would like to know, how the code (see A) works exactly. Why is title in the [ ] on it's own, shouldn't it be [title, rating]?

Thanks

A
movies[title] = rating

B
symbols.push(s.to_sym)


#2

Your query is a little confusing.

That was a .each loop. How do you think it would apply for movies hash?

Using square brackets, we access a particular key from the hash. If we write like this:

hash[key] = value

we set the value corresponding to key in the hash to value. If we write:

a = hash[key]

it will assign the variable a the value corresponding to key in hash.

So, it does not make sense to do something like movies[title, rating] as you are saying :wink: