From 0ed4d84bc6b42b25c1bce41c8eaf35a868dd32a3 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 2 Jul 2025 10:09:48 -0500 Subject: [PATCH] Add Get Specific Values From Hashes And Arrays as a Ruby TIL --- README.md | 3 +- ...-specific-values-from-hashes-and-arrays.md | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ruby/get-specific-values-from-hashes-and-arrays.md diff --git a/README.md b/README.md index c18dd9b..763931e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1650 TILs and counting..._ +_1651 TILs and counting..._ See some of the other learning resources I work on: - [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7) @@ -1345,6 +1345,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Generate A Signed JWT Token](ruby/generate-a-signed-jwt-token.md) - [Generate Ruby Version And Gemset Files With RVM](ruby/generate-ruby-version-and-gemset-files-with-rvm.md) - [Get Info About Your RubyGems Environment](ruby/get-info-about-your-ruby-gems-environment.md) +- [Get Specific Values From Arrays And Hashes](ruby/get-specific-values-from-hashes-and-arrays.md) - [Get The Output Of Running A System Program](ruby/get-the-output-of-running-a-system-program.md) - [Get UTC Offset For Different Time Zones](ruby/get-utc-offset-for-different-time-zones.md) - [Identify Outdated Gems](ruby/identify-outdated-gems.md) diff --git a/ruby/get-specific-values-from-hashes-and-arrays.md b/ruby/get-specific-values-from-hashes-and-arrays.md new file mode 100644 index 0000000..f80cd38 --- /dev/null +++ b/ruby/get-specific-values-from-hashes-and-arrays.md @@ -0,0 +1,44 @@ +# Get Specific Values From Hashes And Arrays + +Ruby defines a `#values_at` method on both `Hash` and `Array` that can be used +to grab multiple values from an instance of either of those structures. + +Here is an example of grabbing values by key (if they exist) from a hash. + +```ruby +> hash = {one: :two, hello: "world", four: 4} +=> {one: :two, hello: "world", four: 4} +> hash.values_at(:one, :four, :three) +=> [:two, 4, nil] +``` + +And here is an example of grabbing values at specific indexes from an array, if +those indexes exist. + +```ruby +> arr = [:a, :b, :c, :d, :e] +=> [:a, :b, :c, :d, :e] +> arr.values_at(0, 3, 6) +=> [:a, :d, nil] +``` + +Notice that in both cases, `nil` is returned for a key or index that doesn't +exist. + +What I like about this method is that in a single call I can grab multiple +named (or indexed) values and get a single array result with those values. + +One way I might use this with a JSON response from an API request could look +like this: + +```ruby +resp = client.getSomeData(id: 123) + +[status, body] = resp.values_at("status", "body") + +if status == 200 + puts body +end +``` + +[source](https://docs.ruby-lang.org/en/3.4/Hash.html#method-i-values_at)