mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Get Specific Values From Hashes And Arrays as a Ruby TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
44
ruby/get-specific-values-from-hashes-and-arrays.md
Normal file
44
ruby/get-specific-values-from-hashes-and-arrays.md
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user