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.
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
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 hasnil
as value. Since the lesson text explicitly discusses problems arising from usingmy_favorties[:number]
to access values of a hash concerningnil
I added a test for this edge case which takes the hashmy_favorites
from above and expectsnil
.