Confused in assigning default value in hash


#1

no_nil_hash=Hash.new("tree")
no_nil_hash={"p" =>"as",
"s" =>"sad",
"f" =>"dsaf"
}
puts no_nil_hash["k"]
It is showing output as nil


#2

That's because you define the no_nil_hash twice:

Once it's an empty hash with a default value of "tree".

Then it's replaced by a filled hash, but with a default value of nil.

So of course when you ask for a non-existing key it will return nil.


#3

ok.it was very helpful..Thank you