14 Becoming More Selective: Why doesnt this work



I tried using the long hand version but it doesn't return the movie with the rating 3.5.

Why does the first line of code below work, but the code block below it doesn't return the rating with a decimal point?

good_movies = movie_ratings.select {|m,r| r > 3}

good_movies = movie_ratings.select do |m,r|
    if r > 3
        puts m


The first line is correct. r > 3 is the selection criterion. The method iterates over the hash and checks each value, then returns only the key-value pairs that meet this condition. Consequently when writing a do loop, we need to let Ruby look after the conditional and the return.

good_movies = movie_ratings.select do |m,r|
    r > 3

Notice we do not include return in this method? The select method does this internally to build a hash, then assigns it to good_movies.


the code have error in my account ?


Not sure how I can help. Post the code and the error message, please. Thank you.


the do/end part is not even necessary. Less is more :slight_smile:

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 {|movie, grade| grade > 3}


It makes no difference. Either form is valid, and one is not preferable over the other. It's the author's choice.


fair enough :):slight_smile: and no code academy, I haven't tried the love button