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

Ruby-code

A collection of 40 posts

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

Flatten A Nested Array To A Specific Depth In Ruby

Use Array#flatten with a depth argument to collapse nested arrays to a precise level — avoiding full flattening when partial structure needs to be preserved.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Deep Merge Two Nested Hashes Without Losing Keys In Ruby

Perform a recursive hash merge that combines nested values instead of overwriting them — useful for configuration merging, settings overrides, and building complex data structures.

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Sort A Hash By Its Values In Ruby

Use sort_by to order a hash’s key-value pairs by value, returning a sorted array of pairs that can be converted back to a hash.

Rajan Bhattarai Rajan Bhattarai 2 min read
Ruby-code

Group Consecutive Elements In An Array By Condition In Ruby

Use chunk_while to group adjacent array elements that share a condition — perfect for run-length encoding, streak detection, and range grouping.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Generate All Permutations Of An Array In Ruby

Produce every possible ordering of array elements using Array#permutation.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Check If Two Strings Are Anagrams In Ruby

Determine whether two strings contain the same characters in any order.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Rotate An Array By N Positions In Ruby

Shift array elements left or right by N positions using Array#rotate.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Remove Duplicate Elements From An Array While Preserving Order In Ruby

Deduplicate an array in insertion order using Array#uniq.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Zip Multiple Arrays Together In Ruby

Combine elements from two or more arrays by position using Array#zip.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Find Duplicate Elements In An Array In Ruby

Detect which values appear more than once in an array using group_by or tally.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Generate A Secure Random Token In Ruby

Generating cryptographically secure random tokens is essential for password reset links, API keys, email verification tokens, and any value that must be unpredictable to an attacker.

Rajan Bhattarai Rajan Bhattarai 1 min read
Ruby-code

Paginate An Array In Ruby Using Each_slice

When displaying large datasets in pages — or processing records in batches — you need to split an array into fixed-size chunks. Ruby’s each_slice handles this cleanly without manual index arithmetic.

Rajan Bhattarai Rajan Bhattarai 1 min read
« Newer Posts Page 2 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! 🚀