By definition, ruby hashes return zero when there is no key. But I need to use a special message instead of zero. Therefore, I use something like this:
val = h['key'].nil? ? "No element present" : h['key']
But this has a serious flaw. If in this case a value of nil is assigned against the key, then "No element present" will also be returned.
Is there any way to achieve this flawlessly?
thank
user572273
source
share