Why is this not working?


#1




Oops, try again. It looks like good_movies doesn't include primer, but it should!

Maybe it's not what the editor is looking for but i wanted only the Keys to be printed instead of the Key with value(looks cleaner).

Primer and all of the v>3 are showing accordingly, but the editor is not accepting it. Why?


Replace this line with your 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.select do |k,v|
    if v > 3
        puts k
    end 
end


#2

select is a conditional method so we don't need to include an if statement. Rather than puts, go with the implicit return.

...select do |k,v|
    v > 3
end

This is not like a return from a function. What gets returned is any key:value with rating greater than 3.


#3

I've included the if because if I don't, all of the keys get returned.

IE
v > 3
puts k

returns all the keys, it doesn't take into account the condition above.

What i want is Only the Key, not the values to be displayed.
Instead of getting x=>3 y=>5 q=>3.5
I want x y and q

In addition to this, i know the solution is {|k.v| v > 3}
I was looking to improve it slightly


#4

Use select to filter results to a new hash, then print off the values in that hash. Treat is like an uncorroborated query.


#5

Can you give me an example?


#6

This is a bit non-standard, one supposes, but it works:

movie_ratings.select { |m,r| r > 3 }.each { |k,v| puts v }
puts

3.5
4
4
3.5

Note that movie_ratings.select { |m,r| r > 3 } is an iterable, that is why we can chain each to it.


#7

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