14. Becoming More Selective


#1


Add your code below!


movie_ratings = {
  memento: 3,
  primer: 3.5,
  the_matrix: 5,
  truman_show: 4,
  red_dawn: 1.5,
  skyfall: 4,
  alex_cross: 2,
  uhf: 1,
  lion_king: 3.5
}
# Add your code below!
good_movies = movie_ratings.select do |m, r|
    r > 3
end

Why is it m and r? How is this relevant to the code?


#2

It could be any name, but m and r are fitting. m => movie, r => rating.

The two are block variables in the scope of the block, and act as placeholders as select iterates over the hash. m is the key, r is the value.


#3

so you can just put random letters?


#4

Thank You for your assistance


#5

Yes, though random and arbitrary are two different things. Arbitrary is usually chosen for the sake of readability. Just randomly applying variable names can be messy and difficult to read (when aiming for relevant meaning).

Variable names that describle what they reference add meaning to code. It's always easier to understand code if we can identify objects and their purpose.


#6

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