FirstDEV blog
  • Home
  • Ruby
  • Ruby On Rails
  • Tips
  • Solutions
  • About
Subscribe

Ruby-code

A collection of 40 posts

Ruby-code

Generate All Combinations Of Array Elements Without Repetition In Ruby

Picking every possible pair of players, every three-item bundle from a catalogue, every subset of feature flags — these are combinations, and Ruby has them built in. No manual nested loops required.

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Parse A Csv String Into An Array Of Hashes With Headers In Ruby

Uploaded spreadsheets and API exports arrive as CSV strings. Converting them into an array of hashes keyed by header name makes the rest of the code readable — row[:email] instead of row[3].

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Count Unique Values Across Nested Arrays In Ruby Without Duplicates

Tags across a set of posts, permissions across a set of roles, skills across a team — the data arrives as an array of arrays and you need the distinct total, or the

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Remove Duplicate Hashes From An Array By A Specific Key In Ruby

uniq on an array of hashes compares whole hashes, so two records that share an id but differ in any other field both survive. Deduplicating by one key — an id, an email,

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Flatten A Hash Of Arrays Into A Flat Array Of Hashes In Ruby

Grouped data — { "ruby" => [post1, post2], "rails" => [post3] } — often needs to be turned back into a flat list where each item carries its group. It is the inverse

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Truncate A String At A Word Boundary In Ruby Without Cutting Mid Word

Cutting a string at a fixed character count produces things like “The quick brown fo…”. Truncating at the last whole word before the limit reads properly and is what you want for previews,

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Extract Unique Values From An Array Of Hashes By A Key In Ruby

You have a list of records and you want every distinct value of one field — all the account ids in a batch, every category present in a result set, the set of

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Retry A Block N Times With Exponential Backoff In Ruby

Network calls, external APIs, and flaky I/O fail intermittently. Retrying immediately usually fails again — the remote service needs a moment. Exponential backoff spaces retries out so each attempt has a better chance

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Build A Hash From Two Parallel Arrays In Ruby

Two arrays — one of keys, one of values, lined up by index — is a shape that comes out of CSV headers plus a row, form field names plus submitted values, and

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Transpose A 2d Array Matrix In Ruby

Flip a matrix over its diagonal — rows become columns and columns become rows. Ruby’s Array#transpose handles this in one call.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Memoize Expensive Method Calls In Ruby To Avoid Repeated Computation

When a method performs an expensive operation — a database query, an API call, a complex calculation — calling it repeatedly wastes time and resources. Memoization caches the result on first call and

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Deep Merge Nested Hashes In Ruby Without Losing Keys

Ruby’s built-in Hash#merge only merges the top level — nested keys get overwritten entirely instead of merged recursively. When working with deeply nested configuration objects or API payloads, you need a merge that

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Diff Two Hashes And Return Added Removed And Changed Keys In Ruby

When comparing configuration objects, API responses, or form submissions, you often need to know exactly what changed — not just whether the hashes are equal.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Find The Longest Consecutive Sequence In An Array In Ruby

Identify the longest run of consecutive integers in an unsorted array — useful for streak detection, range analysis, and data validation.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Convert An Array Of Hashes Into A Grouped Hash By A Key In Ruby

Transform a flat array of hashes into a hash where each key maps to an array of matching records — the core operation for grouping, summarizing, and pivoting collections.

Rajan Bhattarai Rajan Bhattarai 2 min read
Page 1 of 3 Older Posts »
FirstDEV blog © 2026
Proudly published with Jekyll & using Jasper2
Latest Posts Twitter
FirstDEV blog

Check viewARU - Brand Newsletter!

Newsletter to DEVs by DEVs - boost your Personal Brand & career! 🚀