RubyHash Blog
Articles about Ruby, Rails, testing, and developer tooling from the team behind RubyHash.
- Website
- rubyhash.dev · all feeds from this site
- Feed URL
https://rubyhash.dev/rss.xml
Read it in your FeedListing reader
Or add to: Feedly Inoreader Raw feed
Latest posts
-
Ruby tally: Counting Occurrences in Arrays and Hashes Without Building the Hash Yourself
Enumerable#tally counts how often each value appears and returns a hash. Here is how it works, how to count by a derived key, how to sort the result, and when group_by or sum is the better tool.
-
Comparing ActiveRecord attributes to a Hash in Rails Tests: slice, symbolize, and the Types That Bite
Asserting that a record matches an expected hash looks trivial until string keys, timestamps, BigDecimal and enum values break the comparison. Here is how to compare record.attributes against a hash without brittle tests.
-
Hash#compact in Ruby: Removing nil Values, and the Cases It Does Not Cover
Hash#compact strips nil values from a hash, but it is shallow, it keeps false and empty strings, and it changes how two hashes compare. Here is what it does, what it skips, and where compact_blank fits in Rails.
-
Hash#dig in Ruby: Safe Nested Access Without the nil Errors
Hash#dig reads deep into nested hashes and arrays without raising NoMethodError on a missing key. Here is when it helps, where it surprises you, and when fetch is the better tool.
-
JSON.parse with symbolize_names: Symbol Keys, and Everything Else That Does Not Come Back
JSON.parse hands you string keys unless you pass symbolize_names: true, and even then a round trip through JSON is not lossless. Here is what the option does, what it cannot restore, and how to compare parsed JSON in tests.
-
transform_keys and transform_values in Ruby: What They Change and What They Quietly Skip
transform_keys and transform_values are the cleanest way to reshape a Ruby hash, but they are shallow, they drop defaults, and they can collapse keys without warning. Here is how they behave and where they bite.
-
How to Sort a Hash by Key in Ruby (and Why You Get an Array Back)
Calling sort on a Ruby hash hands you an array of pairs, not a hash. Here is why that happens, how to get a sorted hash back, and the cases where sorting actually buys you something.
-
Comparing Arrays of Hashes in Ruby Without Order Ruining It
Comparing two arrays of hashes with == fails the moment the order changes, even when the data is identical. Here is why it happens and the patterns that actually compare the content.
-
Your RSpec Hash Diff Is Unreadable. Here Is How to Fix It
RSpec diffs two hashes by pretty printing both and comparing the text, which is why the output looks like noise. Here is what actually causes it and the ways to get a readable answer.
-
params.to_h Surprises: What Strong Parameters Actually Gives You
ActionController::Parameters is not a Hash, and to_h behaves differently depending on whether you permitted anything. Here is what actually comes back and why your test fails on a hash that looks right.
-
slice and except in Ruby: Keep or Drop Hash Keys Cleanly
Hash#slice keeps the keys you name, except drops them. Reaching for either instead of hand-rolled select or delete_if removes a whole category of small bugs.
-
How to Pretty Print a Hash in Ruby (pp, pretty_generate, and awesome_print)
A single-line inspect of a large Ruby hash is unreadable. Here are the built-in and gem-based ways to pretty print a hash, and when each one is the right call.
-
HashWithIndifferentAccess: The Rails Convenience That Breaks Your Tests
HashWithIndifferentAccess lets you read a key with a string or a symbol, until you compare it to a plain hash in a test and everything falls apart. Here is why it happens and how to compare cleanly.
-
deep_merge in Ruby: Why merge Clobbers Your Nested Hashes
Hash#merge is shallow, so merging nested hashes silently throws away the nested data you meant to keep. Here is why it happens and how deep_merge fixes it.
-
Comparing JSON Responses in Rails Tests Without Losing Your Mind
Asserting that a Rails JSON API returns the right body sounds simple, until the test fails on two blobs that look identical. Here is what actually trips these assertions and how to compare responses cleanly.
-
Why Timestamps Break Hash Equality in Ruby Tests
Your test asserts two hashes are equal, the timestamps look identical, but it still fails. Here is why Ruby time values break hash equality and how to fix it.
-
Minitest Hash Diff Hard to Read? Here's How to Actually Compare Them
When a Minitest assertion fails on two hashes, the output is nearly unreadable. Here's why it happens and a faster way to find the one value that changed.
-
How to Compare Two Ruby Hashes and Find the Difference
Several ways to compare two Ruby hashes and find exactly what changed, from plain Ruby techniques to a visual side-by-side diff, with the tradeoffs of each.
-
Rails Test Failing on a Hash Assertion? A Debugging Checklist
Your Rails test asserts two hashes are equal, they look identical, but the test still fails. Here's a checklist of the usual causes and how to find the real one.
-
Symbol vs String Keys: The Hash Bug That Hides in Plain Sight
In Ruby, :name and "name" are different hash keys, even though they look almost the same. That one distinction is behind a whole category of nil-returning, test-failing bugs.