I'm proposing to add a new method Hash#values_at! that's basically a combination of #values_at and #fetch.
When dealing with configuration I prefer to use #fetch to fail early and this would be useful to read multiple keys at a time.
An example (not strictly on a Hash, but you get the idea) could be:
repo,token=ENV.values_at!('GITHUB_REPO','GITHUB_TOKEN')ENV.values_at!('INVALID')# raises KeyError# not sure if that useful, but I provide it here for the sake of completenessENV.values_at!("SHELL","INVALID"){|k|k+" is missing"}# => ["/bin/bash", "INVALID is missing"]
Another name for this could be #fetch_at, perhaps.
P.S. I'm attaching a patch that was done using mostly trial and error
I attached another patch calling this method fetch_at and I also added missing tests (I couldn't change issue title & body to reflect this changes, though).
Also, perhaps out of scope for this ticket but I'm wondering what're your thoughts about adding #fetch_at to ENV and Array too.
Please consider the name fetch_at carefully: it seems easily confused with fetch (1 key), and the name doesn't hint that it's for multiple keys. (values_at is plural, so it gives a good clue.) Maybe fetch_values?
Please consider the name fetch_at carefully: it seems easily confused with fetch (1 key), and the name doesn't hint that it's for multiple keys. (values_at is plural, so it gives a good clue.) Maybe fetch_values?
Personally, I prefer fetch_at but fetch_values (or fetch_values_at) is also good!