# Make all arrays (in an array) the same length

**URL:** <https://www.ruby-forum.com/t/make-all-arrays-in-an-array-the-same-length/267891>\
**Category:** Uncategorized\
**Created:** [August 14, 2025, 12:28pm UTC](https://www.ruby-forum.com/t/make-all-arrays-in-an-array-the-same-length/267891 "2025-08-14T12:28:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimC](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/timc/32/3629_2.png) [@TimC](https://www.ruby-forum.com/u/TimC)\
**Post date:** [August 14, 2025, 12:28pm UTC](https://www.ruby-forum.com/t/make-all-arrays-in-an-array-the-same-length/267891/1 "2025-08-14T12:28:21Z")

</div>

I have an array of arrays, the ‘inside’ arrays are of different sizes (up to a maximum of 10) (small example, array sizes 7,4,10,2 : [[“0”,“0”,“0”,“0”,“0”,“0”,“0”],[“2”,“0”,“4”,“0”],[“0”,“0”,“0”,“3”,“0”,“0”,“0”,“4”,“0”,“0”],[“0”,“4”] ] . I need a process or method which can make all elements the same (maximum ) size while retaining the values already there, ie final result is to be sizes 10,10,10,10 : [[“0”,“0”,“0”,“0”,“0”,“0”,“0”,“0”,“0”,“0”],[“2”,“0”,“4”,“0”,“0”,“0”,“0”,“0”,“0”,“0”],[“0”,“0”,“0”,“3”,“0”,“0”,“0”,“4”,“0”,“0”],[“0”,“4”,“0”,“0”,“0”,“0”,“0”,“0”,“0”,“0”] ]. It seems to me there should be a very simple way to do this, but I just cant find it! Any help would be much appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jvrc](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/jvrc/32/3366_2.png) [@jvrc](https://www.ruby-forum.com/u/jvrc)\
**Post date:** [August 14, 2025, 1:55pm UTC](https://www.ruby-forum.com/t/make-all-arrays-in-an-array-the-same-length/267891/2 "2025-08-14T13:55:10Z")

</div>

You can achieve what you need with this by joining an array with the default values and then take the first 10 elements:

```auto
def normalize(input)
  empty_array = Array.new(10, "0")
  
  input.map {|array| (array + empty_array)[0..9] }
end

input = [["0","0","0","0","0","0","0"],["2","0","4","0"],["0","0","0","3","0","0","0","4","0","0"],["0","4"] ]
normalize(input)

```

By running it, you’ll get the following output:

```auto
=> [["0", "0", "0", "0", "0", "0", "0", "0", "0", "0"], ["2", "0", "4", "0", "0", "0", "0", "0", "0", "0"], ["0", "0", "0", "3", "0", "0", "0", "4", "0", "0"], ["0", "4", "0", "0", "0", "0", "0", "0", "0", "0"]]

```

---

<div class="post-metadata">

**Author:** ![TimC](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/timc/32/3629_2.png) [@TimC](https://www.ruby-forum.com/u/TimC)\
**Post date:** [August 21, 2025, 10:42pm UTC](https://www.ruby-forum.com/t/make-all-arrays-in-an-array-the-same-length/267891/3 "2025-08-21T22:42:44Z")

</div>

Thanks jvrc, that’s just what I needed!
