Reputation: 593
I'm learning Ruby on Codecademy and I'm having trouble with this problem:
Create a new variable, good_movies, and set it equal to the result of calling .select on movie_ratings, selecting only movies with a rating strictly greater than 3.
Here's my code:
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.each {|k,v| v > 3}
Here is the result:
{: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}
And this is the error that I'm getting:
Oops, try again. It looks like good_movies includes memento, but it shouldn't.
"memento" has a value of 3 and I thought that my "v > 3" condition would filter it out; what am I doing wrong?
Upvotes: 0
Views: 540
Reputation: 118289
Use Hash#select
to filter as per conditions, not Hash#each
.
movie_ratings.select {|k,v| v > 3}
Basically Hash#each
returns the receiver on which you called it, and in your case, it the original hash movie_ratings
. Indeed it contains memento
key, as I said, Hash#each
not for filtering purposes. But Hash#select
, will filter memento
with its value out from the output Hash
, thus your code will not give any objections.
Upvotes: 2
Reputation: 10811
You must have change each
for select
like this: good_movies = movie_ratings.select {|k,v| v > 3}
Upvotes: 1