# Each\_with\_index yields mutable array

**URL:** <https://www.ruby-forum.com/t/each-with-index-yields-mutable-array/119377>\
**Category:** Development\
**Created:** [November 6, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/each-with-index-yields-mutable-array/119377 "2007-11-06T15:44:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [November 6, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/each-with-index-yields-mutable-array/119377/1 "2007-11-06T15:44:43Z")

</div>

e$B1sF#$H?=$7$^$9!#e(B

1.9 e$B$Ge(B Enumearble#each\_with\_index e$B$,e(B yield  
e$B$7$F$/$kG[Ns$,JQ992DG=$G$9!#e(B  
e$BL5BL$KJXMx$+$b$7$l$J$$$N$G$9$,!";EMM$G$7$g$&$+!#e(B

$ ./ruby -e ‘%w(a b c).each\_with\_index {|r| p r; r.push r[0] }’  
[“a”, 0]  
[“b”, 1, “a”]  
[“c”, 2, “a”, “b”]

e$B$3$NG[Ns$r=L$a$?\>l9g!“M}O@\>e%”%/%;%90cH?$9$k2DG=@-$,$"$k$h$&$J5$$,$7$^$9!#e(B  
(e$B8=\<BE\*$K$OH/\>I$7$J$$$H;W$$$^$9$,e(B)

$ ./ruby -e ‘%w(a b c).each\_with\_index {|r| p r; r.pop }’  
[“a”, 0]  
[“b”]  
[]

e$B$H$j$"$($:$3$NG[Ns$re(B freeze e$B$5$;$k%Q%C%A$G$9!#e(B

# Index: enum.c

— enum.c (revision 13827)  
+++ enum.c (working copy)  
@@ -1327,6 +1327,7 @@  
RETURN\_ENUMERATOR(obj, argc, argv);

```
 memo = rb_ary_new3(2, Qnil, INT2FIX(0));

```

- rb\_ary\_freeze(memo);  
rb\_block\_call(obj, id\_each, argc, argv, each\_with\_index\_i, memo);  
return obj;  
}

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [November 7, 2007, 5:11am UTC](https://www.ruby-forum.com/t/each-with-index-yields-mutable-array/119377/2 "2007-11-07T05:11:54Z")

</div>

e$B1sF#$G$9!#e(B

> 1.9 e$B$Ge(B Enumearble#each\_with\_index e$B$,e(B yield e$B$7$F$/$kG[Ns$,JQ992DG=$G$9!#e(B  
> e$BL5BL$KJXMx$+$b$7$l$J$$$N$G$9$,!";EMM$G$7$g$&$+!#e(B

e$BG[Ns$,JQ992DG=$G$"$k$3$H$h$j!";H$$$^$o$5$l$F$$$k$3$H$NJ}$,LdBj$J$N$+$be(B  
e$BCN$l$^$;$s!#0J2\<$O;EMM$G$7$g$&$+!#e(B

$ ./ruby -e ‘p %w(a b c).each\_with\_index.to\_a’  
[[“c”, 3], [“c”, 3], [“c”, 3]]

e$B$A$J$\_$Ke(B each.with\_index  
e$B$N\>l9g$O;d$N4|BTDL$j$N7k2L$K$J$j$^$7$?!#e(B

$ ./ruby -e ‘p %w(a b c).each.with\_index.to\_a’  
[[“a”, 0], [“b”, 1], [“c”, 2]]

# each\_with\_index e$B$Oe(B each.with\_index

e$B$HEy2A$G$"$k$H;W$$$?$$$G$9!#e(B

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [November 9, 2007, 4:48am UTC](https://www.ruby-forum.com/t/each-with-index-yields-mutable-array/119377/3 "2007-11-09T04:48:31Z")

</div>

e$B1sF#$G$9!#e(B

> 1.9 e$B$Ge(B Enumearble#each\_with\_index e$B$,e(B yield e$B$7$F$/$kG[Ns$,JQ992DG=$G$9!#e(B  
> e$BL5BL$KJXMx$+$b$7$l$J$$$N$G$9$,!";EMM$G$7$g$&$+!#e(B

e$B$3$N7o!“e(Brevision 13833  
e$B$GBP1~$$$?$@$$$?$h$&$G$”$j$,$H$&$4$6$$$^$9!#e(B

e$B$7$+$78e$G5$$,$D$$$?$N$G$9$,!“0J2\<$N$h$&$J5sF0$N0c$$$b$”$j$^$7$?!#e(B

$ ./ruby -e ‘%w(a b c).each\_with\_index {|x| p x }’  
[“a”,0]  
[“b”,1]  
[“c”,2]

$ ./ruby -e ‘%w(a b c).each.with\_index {|x| p x }’  
“a”  
“b”  
“c”

13833 e$B$K2C$($F0J2\<$N$h$&$K$9$k$H$h$$$H;W$$$^$9!#e(B

# Index: enum.c

— enum.c (revision 13846)  
+++ enum.c (working copy)  
@@ -1295,7 +1295,7 @@  
{  
long n = (\*(VALUE \*)memo)++;

- return rb\_yield(rb\_ary\_new3(2, val, INT2NUM(n)));

- return rb\_yield\_values(2, val, INT2NUM(n));  
}

/\*
