Question: By definition ruby hashes return nil when a key is not present. But I need to use a custom message in the place of nil. So I'm using something like this:

```
val = h['key'].nil? ? "No element present" : h['key']

```

But this has a serious drawback. If there's a assigned nil against the key this will return "No element present" in that case also.

Is there a way to achieve this flawlessly?

Thanks

Answer: 
```
val = h.has_key?("key") ? h['key'] : "No element present"

```
Initialize your hash this way:

```
> hash = Hash.new{|hash,key| hash[key] = "No element against #{key}"}
 => {}
> hash['a']
 => "No element against a" 
> hash['a'] = 123
 => 123 
> hash['a'] 
 => 123 
> hash['b'] = nil
 => nil 
> hash['b']
 => nil 

```

Hope this helps :)