Why am i getting 'keyerror: key not found'?

Example:

  hash = {name: 'Alice'}
  puts hash[:age]
  
Solution:

This error occurs when you're trying to access a key that doesn't exist in the hash. Ensure the key exists before trying to access it or use a default value.


  puts hash.fetch(:age, 'Key not found')
  

Beginner's Guide to Ruby