Complete programming beginner just trying to clarify something, I was creating keys for a hash and decided I wanted to delete one. Here were my guesses:
hash["key"].delete
hash.delete["key"]
I eventually looked it up and I’m suppose to use ( ):
hash.delete("key")
Since I used a [ ] to create the hash, why wouldn’t I use it to delete a key? Also, and why wouldn’t hash("key").delete work?
[]is actually a method ofHash, and is treated specially in the language so the parameter goes between the[]. It’s equivalent tostore, which uses standard method syntax.Likewise,
deleteis also a method, which takes one argument. Doinghash.delete['foo']is trying to calldelete(with no arguments), and then call[]on whatever it returns. Doinghash['foo'].deleteis callingdeleteon whatever is stored inhash['foo'].