Understanding how to add user input to hash


#1

Hi everyone,

I have an issue in understanding the syntax for adding to a hash .

In night at the movies we use the following code to do so

  movies[title.to_sym] = rating.to_i

On the right side of the equation Tthe idea of adding the title to the hash as a symbol makes sense to me, but why do we make this equal to rating? I can’t make any sense of this.

Thanks for any direction :slight_smile:


#2

That’s not an equation, it’s an instruction, it carries out an action. A hash is a data structure that stores key-value pairs, just like a phone book (name-number pairs)

Also, one shouldn’t internalize user input into symbols, instead one should use frozen strings as keys
(symbols should be used in finite amounts, typically only mentioned directly in code, not dynamically generated)


#3

Which lesson is this, exactly? Please post a link.

A hash is akin to an associative array whereby movie ratings are associated to their film title.

title: rating

Part of the exercise, not a learner choice.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.