---
title: "Discussion on \"Checking for the existence of a Hash key\" | Hashnode"
description: "Discussion on \"Checking for the existence of a Hash key\". You can test whether a key is present in a hash and return a precise message when it fails thanks to the KeyError exception raised by #fetch:\nbegin\n  {one: \"1\", two: \"2\"}.fetch(:three)\nrescue => e\n  p \"The key #{e.key.inspect} cannot be found in #{e...."
url: "https://hashnode.com/posts/checking-for-the-existence-of-a-hash-key/692ffc45f739b8a7c63f8c24"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Checking for the existence of a Hash key](https://langage-ruby.hashnode.dev/checking-for-the-existence-of-a-hash-key?utm_source=hashnode&utm_medium=feed)

You can test whether a key is present in a hash and return a precise message when it fails thanks to the KeyError exception raised by #fetch: begin {one: "1", two: "2"}.fetch(:three) rescue => e p "The key #{e.key.inspect} cannot be found in #{e....

Dec 3, 2025 · 1 min read · langage-ruby.hashnode.dev  
Read the article

-   [#ruby](https://hashnode.com/tag/ruby)
-   [#usa](https://hashnode.com/tag/usa)
-   [#en](https://hashnode.com/tag/en)
-   [#hash](https://hashnode.com/tag/hash)
-   [#key](https://hashnode.com/tag/key)

## Written by

[Philicare](https://hashnode.com/@philicare)

@philicare

  
[View profile](https://hashnode.com/@philicare)

Share and learn

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
