TheOdinProject / ruby-exercises

MIT License
219 stars 1.06k forks source link

fix: Add test to favorite number exercise #68

Closed lynn-otto closed 2 years ago

lynn-otto commented 2 years ago

Before adding the new test to /ruby_basics/7_hashes it was possible to pass all tests of the "favorite number" exercise by writing the method

def favorite_number(favorite_list)
  # return the value of the number key or 42 if the key is not found
  result = favorite_list[:number]
  if result == nil
    return 42
  else
    return result
  end
end

This solution leads to the unwanted behavior that it returns 42 for a hash like my_favorites = { color: ['orange', 'green'], number: nil } where the :number key is present but has nil as value. Since the lesson text explicitly discusses problems arising from using my_favorties[:number] to access values of a hash concerning nil I added a test for this edge case which takes the hash my_favorites from above and expects nil.

lynn-otto commented 2 years ago

I see, sounds good! Thanks for the feedback. I changed it accordingly.