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

Ruby-code

A collection of 31 posts

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
Ruby-code

Find All Pairs In An Array That Sum To A Target Value In Ruby

Return every pair of distinct elements that add up to a target — a classic interview problem with an efficient hash-based O(n) solution.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Calculate A Running Total Of An Array In Ruby

Compute cumulative sums (running totals) — each element in the result is the sum of all preceding elements plus itself. Useful for financial calculations, progress tracking, and time series data.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Find The Most Frequently Occurring Element In An Array In Ruby

Count element occurrences and return the one that appears most often — useful for analytics, voting systems, and mode calculations.

Rajan Bhattarai Rajan Bhattarai 1 min read
Page 1 of 4 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! 🚀