# Loop over an array that changes the elements

**URL:** <https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323>\
**Category:** Ruby\
**Created:** [July 13, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323 "2007-07-13T13:31:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/1 "2007-07-13T13:31:36Z")

</div>

Hi,

I have a rather simple question. I have an array and want to change the  
elements of the array in a loop. For example in Java, you would write:

int[] numbers = {1, 2, 3}

for(int i = 0; i \< numbers.length; i++)  
{  
numbers[i] = numbers[i] \* 2  
}

In Ruby the common way to loop over an array is Array#each

But

numbers = [1, 2, 3]

numbers.each do |num|  
num = num \* 2  
end

would not change the array itself.

Of course, you can write something like

for i in 0 … numbers.length-1 do  
numbers[i] = numbers \* 2  
end

But this in my eyes is not really the Ruby way. Is there a standard way  
of doing this?

Thx a lot!

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/2 "2007-07-13T13:57:17Z")

</div>

On 7/13/07, Bernd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }

numbers.map!{ |x| x\*2 }  
HTH  
Robert

> would not change the array itself.  
> No it would point num to a new Integer simply, you can only change the  
> objects num refers to, but in our case as these objects are immutable  
> Integers it wont work.  
> Consider Strings which are muteable in ruby, and try this code ( best in  
> irb )  
> letters=%w{a b c}  
> letters.each do | letter |  
> letter \<\< “\*”  
> end

Still I prefer  
letters.map{ |l| l \<\< “_" }  
although  
letters.map!{ |l| l + "_” }  
is probably much cleaner code 🙂

HTH  
Robert

> Of course, you can write something like
> 
> for i in 0 … numbers.length-1 do  
> numbers[i] = numbers \* 2  
> end

> But this in my eyes is not really the Ruby way. Is there a standard way  
> of doing this?  
> Well spotted Bernd 😉
> 
> Thx a lot!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/3 "2007-07-13T14:54:28Z")

</div>

> }  
> end  
> of doing this?  
> numbers = numbers.map {|number| numer \* 2}

Cheers,  
B

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/4 "2007-07-13T15:01:29Z")

</div>

El Jul 13, 2007, a las 2:53 PM, removed\_email\_address@domain.invalid  
escribió:

> numbers = numbers.map {|number| numer \* 2}

Even, to be closer to the in-place editing,

numbers.map! {|n| n\*2}

– fxn

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/5 "2007-07-13T14:59:29Z")

</div>

> }  
> end
> 
> would not change the array itself.

You’re right.

As others and I have said, the map solution is good here.

numbers = numbers.map {|number| number\*2}

If you really wanted to change the array itself though, you could do  
this:

numbers.each\_index {|i| numbers[i] = numbers[i] \* 2}

or

numbers.each\_index {|i| numbers[i] \*= 2}

Thanks,  
Benj

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/6 "2007-07-13T15:03:29Z")

</div>

On 7/13/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > }  
> > end  
> > of doing this?
> 
> numbers = numbers.map {|number| numer \* 2}  
> Hmm what about the GC?

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/7 "2007-07-13T15:27:34Z")

</div>

On 7/13/07, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, as I think someone already posted, map! is probably the most  
> idiomatic way of doing this:

Daniel this is great I am _not_ in your killfile 🙂

Robert

---

<div class="post-metadata">

**Author:** ![bernd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/977dab/32.png) [@bernd](https://www.ruby-forum.com/u/bernd)\
**Post date:** [July 13, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/loop-over-an-array-that-changes-the-elements/109323/8 "2007-07-13T15:18:32Z")

</div>

removed\_email\_address@domain.invalid writes:

> As others and I have said, the map solution is good here.
> 
> numbers = numbers.map {|number| number\*2}
> 
> If you really wanted to change the array itself though, you could do this:
> 
> numbers.each\_index {|i| numbers[i] = numbers[i] \* 2}

Actually, as I think someone already posted, map! is probably the most  
idiomatic way of doing this:

numbers.map! {|x| 2\*x}
