I have a Ruby hash that looks like this:
h = {"side1"=>["green", "Green"], "side2"=>["verde", "Verde"]}
How can I get the first (or last) item at a particular key in the hash?
I ran into a similar situation where I arbitrarily wanted the "first" entry from the hash for some test code. That's not exactly what this question is about, but it came up in the search I ran so figure it might do likewise for others.
Of course "first item" is arbitrary in a hash, but the keys method will return a list of all keys sorted by some magic. If you like that sort order, then you can get the "first" method with
h[h.keys.first]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With