Callcc and Enumerable#each_with_index


#1

e$B$3$s$P$s$O!"e(Bsheepman e$B$G$9!#e(B

callcc e$B$He(B Enumerable#each_with_index e$B$r$$$C$7$g$K;H$&$He(B
index e$B$,$*$+$7$/$J$j$^$9!#e(B

$ cat t.rb
vv = 0
[1, 2, 3].each_with_index{|i, idx|
callcc {|k| $k = k} if i == 2
vv += 1
p [vv, idx]
}
$k.call if vv < 7

$ ruby-1.9 t.rb
[1, 0]
[2, 1]
[3, 2]
[4, 1]
[5, 4]
[6, 1]
[7, 6]

e$B0J2<$O%Q%C%A$G$9!#e(B

$ svn diff enum.c
Index: enum.c

— enum.c (revision 12415)
+++ enum.c (working copy)
@@ -1167,11 +1167,10 @@
each_with_index_i(VALUE val, VALUE memo)
{
long n;

  • VALUE idx = RARRAY_PTR(memo)[1];
    RARRAY_PTR(memo)[0] = val;
    rb_yield(memo);
  • val = RARRAY_PTR(memo)[1];
  • n = NUM2LONG(val);
  • n = NUM2LONG(idx);
    n++;
    RARRAY_PTR(memo)[1] = INT2NUM(n);
    return Qnil;

#2

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:30846] callcc and Enumerable#each_with_index”
on Wed, 30 May 2007 21:55:19 +0900, sheepman
removed_email_address@domain.invalid writes:

|callcc e$B$He(B Enumerable#each_with_index e$B$r$$$C$7$g$K;H$&$He(B
|index e$B$,$*$+$7$/$J$j$^$9!#e(B

e$B%Q%C%A$"$j$,$H$&$4$6$$$^$7$?!#$5$-$[$I<h$j9~$_$^$7$?!#e(B