Can anyone tell me how I can delete the value “key” from this hash?
Thanks!
stats = {
“score” => 100,
“weapon” => “sword”,
“items” => [“torch”, “rope”, “key”, “bucket”]
}
Can anyone tell me how I can delete the value “key” from this hash?
Thanks!
stats = {
“score” => 100,
“weapon” => “sword”,
“items” => [“torch”, “rope”, “key”, “bucket”]
}
There is no value “key” in that hash. There is a string “key” that
exists in an array associated with the key “items” in that hash.
If you know the key is “items” and you know that the element you want to
delete from the array associated with the key “items” is the string
“key” then you could simply do the following:
stats.update(“items” => stats[“items”].reject{|item| item == “key”})
Scott S. wrote in post #1170077:
There is no value “key” in that hash. There is a string “key” that
exists in an array associated with the key “items” in that hash.If you know the key is “items” and you know that the element you want to
delete from the array associated with the key “items” is the string
“key” then you could simply do the following:stats.update(“items” => stats[“items”].reject{|item| item == “key”})
Thank you! I searched for hours for how to do this. Your explanation
makes perfect sense.
Best regards.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs