From 8060163d70c05ea183f0b886c9234b558380795e Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 21 Jul 2026 11:43:06 -0500 Subject: [PATCH] Add Use Slice To Reorder Hash Keys as a Ruby TIL --- README.md | 3 ++- ruby/use-slice-to-reorder-hash-keys.md | 34 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 ruby/use-slice-to-reorder-hash-keys.md diff --git a/README.md b/README.md index 9d156d3..b470719 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ruby/use-slice-to-reorder-hash-keys.md b/ruby/use-slice-to-reorder-hash-keys.md new file mode 100644 index 0000000..ccf9827 --- /dev/null +++ b/ruby/use-slice-to-reorder-hash-keys.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.