mirror of
https://github.com/jbranchaud/til
synced 2026-09-01 01:01:46 +00:00
Add Use Slice To Reorder Hash Keys as a Ruby TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1828 TILs and counting..._
|
||||
_1829 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1587,6 +1587,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Use A Case Statement As A Cond Statement](ruby/use-a-case-statement-as-a-cond-statement.md)
|
||||
- [Use dotenv In A Non-Rails Project](ruby/use-dotenv-in-a-non-rails-project.md)
|
||||
- [Use Rescue As Part Of Inline Statement](ruby/use-rescue-as-part-of-inline-statement.md)
|
||||
- [Use Slice To Reorder Hash Keys](ruby/use-slice-to-reorder-hash-keys.md)
|
||||
- [Use Tap For Better Test Data Setup](ruby/use-tap-for-better-test-data-setup.md)
|
||||
- [Using BCrypt To Create And Check Hashed Passwords](ruby/using-bcrypt-to-create-and-check-hashed-passwords.md)
|
||||
- [What To Do When You Don't Rescue](ruby/what-to-do-when-you-dont-rescue.md)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Use Slice To Reorder Hash Keys
|
||||
|
||||
I recently ran into some code that was building up a hash keyed by _category_.
|
||||
It was a series of chained method calls building and transforming to produce the
|
||||
hash. It then ended with `.slice(*CATEGORIES)`.
|
||||
|
||||
That `#slice` call at the end was doing two things:
|
||||
|
||||
1. It was removing any key-value pairs not in `CATEGORIES`
|
||||
2. It was reordering the hash keys based on the order they appeared in `CATEGORIES`
|
||||
|
||||
It was this second behavior that surprised me.
|
||||
|
||||
Let's look at a minimum example of this:
|
||||
|
||||
```ruby
|
||||
> CATEGORIES = [:books, :clothes, :glassware]
|
||||
=> [:books, :clothes, :glassware]
|
||||
> items_by_category = { glassware: [1,2,3], media: [:a, :b, :c], books: [:e, 3, :g], clothes: [4, 5] }
|
||||
=> {glassware: [1, 2, 3], media: [:a, :b, :c], books: [:e, 3, :g], clothes: [4, 5]}
|
||||
> items_by_category.slice(*CATEGORIES)
|
||||
=> {books: [:e, 3, :g], clothes: [4, 5], glassware: [1, 2, 3]}
|
||||
```
|
||||
|
||||
- I first define `CATEGORIES` which is an (ordered) array of symbols that
|
||||
represent the categories I care about.
|
||||
- Then I manufacture a hash of items by category. This is a bunch of dummy data,
|
||||
including extra categories, meant to demonstrate what `#slice` can do.
|
||||
- Lastly I call `.slice(*CATEGORIES)` on this hash which both pairs it down to
|
||||
that set of categories and reorders the hash so that the keys appears in the
|
||||
same order they do in the `CATEGORIES` array.
|
||||
|
||||
This might be useful if I've defined `CATEGORIES` to be in a specific display
|
||||
order and I'm preparing this hash for consumption in some UI layer.
|
||||
Reference in New Issue
Block a user