# Enumerator#peek

Enumerator#peek [email protected]_\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

Enumerator
e\$B\$+\$i%G!<%?\$rFI\$=P\$9\$H\$-!"@hFI\$\$r\$7\$?\$/\$J\$k\$3\$H\$,\$"\$j\$^\$9!#e(B

e\$B\$?\$H\$(\$P!"%=!<%H:Q\$_\$NC18l\$NNs\$,\$"\$C\$?\$H\$-\$K!"3FC18l\$N?t\$r5ae(B
e\$B\$a\$?\$\$\$H\$9\$k\$H!"0J2<\$N\$h\$&\$K<!\$NMWAG\$H\$NHf3S\$r\$7\$?\$/\$J\$j\$^\$9!#e(B

a = %w[banana banana durian orange orange orange]
e = a.to_enum
n = 0
loop {
word = e.next
n += 1
if e\$B<!\$NMWAG\$,B8:\$7\$Fe(B word e\$B\$H\$O0[\$J\$k\$+!"B8:\$7\$J\$\$e(B
puts “#{word} #{n}”
word = nil
n = 0
end
}

e\$B\$7\$+\$7!"\$3\$3\$Ge(B Enumerator#next
e\$B\$O;H\$(\$^\$;\$s!#;H\$C\$F\$7\$^\$&\$H!"e(B
e\$B%k!<%W\$N<!\$N2s\$G07\$&\$Y\$-MWAG\$r>CHq\$7\$F\$7\$^\$\$\$^\$9!#e(B

e\$B\$+\$H\$\$\$C\$F!"%k!<%W\$N<!\$N2s\$G=hM}\$9\$k\$h\$&\$K\$9\$k\$H!"%k!<%W\$N\$Re(B
e\$B\$H\$^\$o\$j\$4\$H\$K\$R\$H\$D\$NMWAG\$r=hM}\$9\$k\$H\$\$\$&9=B\$\$,2u\$l\$F\$o\$+\$je(B
e\$B\$K\$/\$/\$J\$j\$^\$9!#\$^\$?!":G8e\$NC18l\$N=hM}\$r%k!<%W\$,=*\$o\$C\$?8e\$Ke(B
e\$B\$9\$k\$3\$H\$K\$J\$C\$F!"3FC18lKh\$K9T\$&=hM}\$,%k!<%W\$NCf\$H8e\$K\$U\$?\$De(B
e\$BI,MW\$K\$J\$C\$F\$7\$^\$\$\$^\$9!#\$?\$H\$(\$P!"0J2<\$N\$h\$&\$K=q\$1\$^\$9\$,!“e(B
e\$B%3!<%I\$N=EJ#\$,@8\$8!”\$^\$?Fq\$7\$/\$J\$C\$F\$\$\$^\$9!#e(B

% ./ruby -e ’
a = %w[banana banana durian orange orange orange]
e = a.to_enum
n = 0
prev = nil
loop {
word = e.next
if prev && prev != word
puts “#{prev} #{n}”
n = 0
end
prev = word
n += 1
}
if prev
puts “#{prev} #{n}”
end

banana 2
durian 1
orange 3

# e\$B%l!<%/\$C\$F\$\$\$&\$s\$G\$9\$M!#e(B

e\$B\$=\$3\$G!“MWAG\$r>CHq\$;\$:\$KFI\$=P\$9!"\$D\$^[email protected]\$\$r\$9\$k%a%=%C%Ie(B
e\$B\$,\$”\$l\$P!":G=i\$K=q\$\$\$?!"%k!<%W\$N\$R\$H\$^\$o\$j\$4\$H\$K\$R\$H\$D\$NMWAGe(B
e\$B\$r=hM}\$9\$k\$H\$\$\$&7A\$G5-=R\$G\$-\$^\$9!#\$=\$&\$\$\$&%a%=%C%I\$re(B
Enumerator#peek e\$B\$H\$9\$k\$H!"0J2<\$N\$h\$&\$K=q\$1\$^\$9!#e(B

% ./ruby -e ’
a = %w[banana banana durian orange orange orange]
e = a.to_enum
n = 0
loop {
word = e.next
n += 1
if begin word != e.peek; rescue StopIteration; true end
puts “#{word} #{n}”
word = nil
n = 0
end
}

banana 2
durian 1
orange 3

e\$B\$^\$!!"e(Brescue e\$B\$9\$k\$N\$O8+6l\$7\$\$\$N\$G!"0z?t\$r;XDj\$7\$?\$ie(B
StopIteration e\$BNc30\$N\$+\$o\$j\$K0z?t\$rJV\$9\$h\$&\$K\$7\$?\$i\$\$\$\$\$+\$J!“e(B
e\$B\$J\$I\$H\$\$\$&\$3\$H\$b9M\$(\$i\$l\$^\$9\$,!“4pK\E*\$J%”%\$%G%”\$H\$7\$F\$O\$I\$&e(B
e\$B\$G\$7\$g\$&\$+!#e(B

e\$B\$3\$3\$G!“MWAG\$NJB\$S\$+\$i\$3\$&\$d\$C\$F>pJs\$r\$^\$H\$a\$F\$\$\$/\$H\$\$\$&\$N\$O!“e(B
%w[banana banana durian orange orange orange]
e\$B\$H\$\$\$&J?C3\$J9=B\$\$+\$ie(B
[%w[banana banana], %w[durian], %w[orange orange orange]]
e\$B\$H\$\$\$&[email protected][email protected]\$H\$b\$\$J\$;\$^\$9\$N\$G!"@hFI\$\$,I,e(B
e\$BMW\$K\$J\$k\$H\$\$\$&\$N\$O\$h\$/\$”\$k\$b\$N\$G\$”\$k\$HA[Dj\$G\$-\$^\$9!#\$=\$7\$F!“e(B
[email protected]\$+\$i\$NN`?d\$H\$7\$F!"@hFI\$_\$O\$R\$H\$D\$G:Q\$`\$3\$H\$,B?\$\$!”\$He(B
e\$B\$\$\$&\$3\$H\$bM=A[\$G\$-\$^\$9!#\$D\$^\$je(B Enumerable#peek [email protected](B
e\$BFI\$\$rDs6!\$9\$k\$3\$H\$+\$i!"\$h\$/\$"\$k>u67\$KBP1~\$G\$-!"\$+\$D!"\$3\$l\$he(B
e\$B\$je(B (e\$BJ#[email protected]\$
\$rDs6!\$9\$k\$h\$&\$Je(B)
e\$B0lHL2=\$7\$F\$b\$=\$l\$[\$I1W\$O\$Je(B
e\$B\$\$!"\$H\$\$\$&;EMM\$K\$J\$C\$F\$\$\$k\$H\$\$\$(\$k\$N\$G\$O\$J\$\$\$G\$7\$g\$&\$+!#e(B

e\$B\$J\$*!"\$A\$g\$C\$H\$7\$?I{:nMQ\$H\$7\$F!"e(BStopIteration
e\$B\$,H/@8\$7\$?8e\$K!"e(B
Enumerator#next [email protected];~!“e(B(e\$B7+\$jJV\$7\$N:G=i\$KLa\$k\$N\$G\$O\$J\$/e(B)
e\$B\$:\$C\$He(B StopIteration e\$B\$,H/@8\$9\$k\$h\$&\$KJQ\$(\$F\$”\$j\$^\$9!#e(B

e\$B\$3\$l\$O!“0J2<\$N\$h\$&\$JM}M3\$,\$”\$j\$^\$9!#e(B

• ARGF e\$B\$de(B tty e\$B\$Je(B IO
e\$B\$J\$I\$N7P83\$+\$i=*N;\$OJ#?t2s8!=P\$G\$-\$?\$[\$&e(B
e\$B\$,NI\$\$e(B
• peek e\$B\$GH/@8\$7\$?e(B StopIteration e\$B\$G:G=i\$KLa\$i\$l\$k\$H:\$\$ke(B
• IO e\$B\$r85\$K:n\$C\$?e(B Enumerator e\$B\$G\$O\$b\$H\$b\$H\$=\$&\$J\$ke(B
• 1.8 e\$B\$Ne(B Generator e\$B\$G\$O\$=\$&\$J\$ke(B
• e\$BK\Ev\$K7+\$jJV\$7\$N:G=i\$KLa\$j\$?\$\$\$J\$ie(B rewind e\$B\$,\$"\$j!"\$=\$l\$Oe(B
IO e\$B\$G\$be(B (seek e\$B\$G\$-\$l\$Pe(B) e\$BF0\$/e(B

# % svn diff --diff-cmd diff -x ‘-u -p’ Index: enumerator.c

— enumerator.c (revision 24304)
+++ enumerator.c (working copy)
@@ -32,6 +32,7 @@ struct enumerator {
VALUE args;
VALUE fib;
VALUE dst;

VALUE no_next;
};

@@ -59,6 +60,7 @@ enumerator_mark(void *p)
rb_gc_mark(ptr->args);
rb_gc_mark(ptr->fib);
rb_gc_mark(ptr->dst);

}

static struct enumerator *
@@ -281,6 +283,7 @@ enumerator_init(VALUE enum_obj, VALUE ob
if (argc) ptr->args = rb_ary_new4(argc, argv);
ptr->fib = 0;
ptr->dst = Qnil;

ptr->no_next = Qfalse;

return enum_obj;
@@ -361,6 +364,7 @@ enumerator_init_copy(VALUE obj, VALUE or
ptr1->meth = ptr0->meth;
ptr1->args = ptr0->args;
ptr1->fib = 0;

return obj;
}
@@ -519,6 +523,7 @@ next_init(VALUE obj, struct enumerator *
VALUE curr = rb_fiber_current();
e->dst = curr;
e->fib = rb_fiber_new(next_i, obj);

}

/*
@@ -526,8 +531,8 @@ next_init(VALUE obj, struct enumerator *

• e.next => object
• Returns the next object in the enumerator, and move the internal
• position forward. When the position reached at the end, internal
• position is rewound then StopIteration is raised.
• position forward. When the position reached at the end,
StopIteration
• is raised.
• Note that enumeration sequence by next method does not affect other
• non-external enumeration methods, unless underlying iteration
@@ -540,6 +545,16 @@ enumerator_next(VALUE obj)
{
struct enumerator *e = enumerator_ptr(obj);
VALUE curr, v;
• if (e->lookahead != Qundef) {

• ``````   v = e->lookahead;
``````
• ``````   e->lookahead = Qundef;
``````
• ``````   return v;
``````
• }

• if (e->no_next)

• rb_raise(rb_eStopIteration, “iteration reached at end”);

• curr = rb_fiber_current();

if (!e->fib || !rb_fiber_alive_p(e->fib)) {
@@ -550,7 +565,7 @@ enumerator_next(VALUE obj)
if (e->no_next) {
e->fib = 0;
e->dst = Qnil;

• e->no_next = Qfalse;
rb_raise(rb_eStopIteration, “iteration reached at end”);
}
return v;
@@ -558,6 +573,32 @@ enumerator_next(VALUE obj)

/*

• call-seq:
• e.peek => object
• Returns the next object in the enumerator, but don’t move the
internal
• position forward. When the position reached at the end,
StopIteration
• is raised.
• */

+static VALUE
+enumerator_peek(VALUE obj)
+{

• struct enumerator *e = enumerator_ptr(obj);
• VALUE v;
• if (e->lookahead != Qundef) {
• ``````   v = e->lookahead;
``````
• ``````   return v;
``````
• }
• v = enumerator_next(obj);
• return v;
+}

+/*

• call-seq:

• e.rewind => e

• Rewinds the enumeration sequence by the next method.
@@ -575,6 +616,7 @@ enumerator_rewind(VALUE obj)

e->fib = 0;
e->dst = Qnil;

e->no_next = Qfalse;
return obj;
}
@@ -868,6 +910,7 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, “with_index”,
enumerator_with_index, -1);
rb_define_method(rb_cEnumerator, “with_object”,
enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, “next”, enumerator_next, 0);
• rb_define_method(rb_cEnumerator, “peek”, enumerator_peek, 0);
rb_define_method(rb_cEnumerator, “rewind”, enumerator_rewind, 0);
rb_define_method(rb_cEnumerator, “inspect”, enumerator_inspect, 0);

# Index: test/ruby/test_enumerator.rb

— test/ruby/test_enumerator.rb (revision 24304)
+++ test/ruby/test_enumerator.rb (working copy)
@@ -130,5 +130,27 @@ class TestEnumerator < Test::Unit::TestC
assert_equal(3, e.next)
assert_raise(StopIteration) { e.next }
end
+

• def test_peek
• a = [1]
• e = a.each
• assert_equal(1, e.peek)
• assert_equal(1, e.peek)
• assert_equal(1, e.next)
• assert_raise(StopIteration) { e.peek }
• assert_raise(StopIteration) { e.peek }
• end
• def test_next_after_stopiteration
• a = [1]
• e = a.each
• assert_equal(1, e.next)
• assert_raise(StopIteration) { e.next }
• assert_raise(StopIteration) { e.next }
• e.rewind
• assert_equal(1, e.next)
• assert_raise(StopIteration) { e.next }
• assert_raise(StopIteration) { e.next }
• end
end

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev:38932] the message: “[ruby-dev:38932] Enumerator#peek”,
on Jul/28 18:45(JST) Tanaka A. writes:

Enumerator#peek [email protected]_\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$3\$H\$5\$ie(B, peeke\$B\$KH?BP\$9\$k\$o\$1\$G\$b\$J\$\$\$G\$9\$,e(B…

e\$B\$"\$k%0%k!<%W\$KJ,\$1\$F=87W\$7\$?\$\$\$J\$ie(B, e\$B2<5-\$N\$h\$&\$JJ}\$,e(B Ruby
e\$B\$]\$\$\$+\$be(B.

a = %w[banana banana durian orange orange orange]
a.group_by{|w| w}.each{|key, values| puts “#{key} #{values.size}”}

MapReducee\$B7O\$G\$O>e5-\$N\$h\$&\$J46\$8\$G=q\$/\$3\$H\$,B?\$\$\$H;W\$\$\$^\$9e(B.

e\$B<jA0\$_\$=\$NOC\$G\$9\$,e(B, fairye\$B\$G\$Oe(B
groupinge\$B\$7\$?7k2L\$re(Binjecte\$B\$9\$k=hM}\$,\$+\$J\$je(B
e\$BB?\$\$\$N\$G\$Oe(B? e\$B\$H\$\$\$&OC\$K\$J\$C\$F\$\$\$Fe(B,
inject_by(e\$B2>L>e(B)e\$B\$rF3F~\$7\$h\$&\$H\$\$\$&OC\$Ke(B
e\$B\$J\$C\$F\$\$\$^\$9e(B. e\$B\$=\$&\$7\$?>l9ge(B, Rubye\$BN.\$K=q\$1\$Pe(B:

a = %w[banana banana durian orange orange orange]
a.inject_by(proc{|w| w}){|key, values| puts “#{key} #{values.size}”}

e\$B\$N\$h\$&\$J46\$8\$N5-=R\$K\$J\$kM=Dj\$G\$9e(B.

e\$B\$^\$?e(B, each_conse\$B\$rMQ\$\$\$Fe(B

a = %w[banana banana durian orange orange orange]
a.push nil #-------------------------------------------(1)
e = a.to_enum
n = 0
e.each_cons(2) do |word, peek1|
n += 1
if word != peek1
puts “#{word} #{n}”
word = nil
n = 0
end
end

e\$B\$NMM\$K\$b=q\$1\$^\$9e(B. (1)e\$B\$,L#A9\$J\$s\$G\$9\$,e(B…
(1)e\$B\$7\$J\$/\$F\$b\$A\$c\$s\$H=hM}\$7\$Fe(B
e\$B\$/\$l\$ke(Beach_with_peeke\$B8+\$?\$\$\$N\$,\$"\$C\$F\$b\$h\$\$\$+\$be(B?

__
---------------------------------------------------->> [email protected](B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev:38933] the message: “[ruby-dev:38933] Re:
Enumerator#peek”, on Jul/28 23:36(JST) [email protected]=<ye(B writes:

e\$B<jA0\$_\$=\$NOC\$G\$9\$,e(B, fairye\$B\$G\$Oe(B groupinge\$B\$7\$?7k2L\$re(Binjecte\$B\$9\$k=hM}\$,\$+\$J\$je(B
e\$BB?\$\$\$N\$G\$Oe(B? e\$B\$H\$\$\$&OC\$K\$J\$C\$F\$\$\$Fe(B, inject_by(e\$B2>L>e(B)e\$B\$rF3F~\$7\$h\$&\$H\$\$\$&OC\$Ke(B
e\$B\$J\$C\$F\$\$\$^\$9e(B. e\$B\$=\$&\$7\$?>l9ge(B, Rubye\$BN.\$K=q\$1\$Pe(B:

a = %w[banana banana durian orange orange orange]
a.inject_by(proc{|w| w}){|key, values| puts “#{key} #{values.size}”}

e\$B\$N\$h\$&\$J46\$8\$N5-=R\$K\$J\$kM=Dj\$G\$9e(B.

e\$B\$3\$l4V0c\$\$\$G\$7\$?e(B. e\$B0J2<\$N\$h\$&\$J46\$8\$K\$J\$kM=Dj\$G\$9e(B.

a = %w[banana banana durian orange orange orange]
a.inject_by(proc{|w| w}){|key, sum, value| sum += value}.each{|key,
sum| puts “#{key} #{sum}”}

__
---------------------------------------------------->> [email protected](B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

In article [email protected],
[email protected] ([email protected]=<ye(B) writes:

e\$B\$"\$k%0%k!<%W\$KJ,\$1\$F=87W\$7\$?\$\$\$J\$ie(B, e\$B2<5-\$N\$h\$&\$JJ}\$,e(B Ruby e\$B\$]\$\$\$+\$be(B.

a = %w[banana banana durian orange orange orange]
a.group_by{|w| w}.each{|key, values| puts “#{key} #{values.size}”}

MapReducee\$B7O\$G\$O>e5-\$N\$h\$&\$J46\$8\$G=q\$/\$3\$H\$,B?\$\$\$H;W\$\$\$^\$9e(B.

group_by e\$B\$O;DG0\$J\$3\$H\$K\$<\$s\$V%a%b%j\$KFI\$_9~\$`\$3\$H\$K\$J\$k\$s\$Ge(B
e\$B\$9\$h\$M!#e(B

e\$B%=!<%H:Q\$_\$G\$"\$k\$3\$H\$rA0Ds\$H\$7\$F%7!<%1%s%7%c%k\$K=hM}\$G\$-\$k%ae(B
e\$B%=%C%I\$bEvA3\$"[email protected]\$F!"\$=\$l\$O\$=\$l\$G9M\$(\$F\$\$\$^\$9!#e(B
[ruby-dev:38392] e\$B\$+\$i;O\$^\$k%9%l%C%I\$G!"\$A\$g\$C\$H\$[\$C\$?\$i\$+\$7e(B
e\$B\$K\$7\$F\$\$\$k\$N\$G\$9\$,!“K:\$l\$F\$\$\$k\$o\$1\$G\$O\$”\$j\$^\$;\$s!#e(B

e\$B\$=\$&\$\$\$&!"%7!<%1%s%7%c%k\$K=hM}\$9\$kA`:n\$NCf\$G!“e(Bpeek e\$B\$O\$+\$J\$je(B
e\$BDc5i\$J\$H\$3\$m\$r;Y1g\$9\$kOC\$K\$”\$?\$j\$^\$9!#e(B

[email protected]\$C\$F\$\$\$k\$H\$3\$m\$,0c\$&46\$8\$G\$7\$g\$&\$+!#e(B

e\$B<jA0\$_\$=\$NOC\$G\$9\$,e(B, fairye\$B\$G\$Oe(B groupinge\$B\$7\$?7k2L\$re(Binjecte\$B\$9\$k=hM}\$,\$+\$J\$je(B
e\$BB?\$\$\$N\$G\$Oe(B? e\$B\$H\$\$\$&OC\$K\$J\$C\$F\$\$\$Fe(B, inject_by(e\$B2>L>e(B)e\$B\$rF3F~\$7\$h\$&\$H\$\$\$&OC\$Ke(B
e\$B\$J\$C\$F\$\$\$^\$9e(B. e\$B\$=\$&\$7\$?>l9ge(B, Rubye\$BN.\$K=q\$1\$Pe(B:

a = %w[banana banana durian orange orange orange]
a.inject_by(proc{|w| w}){|key, values| puts “#{key} #{values.size}”}

e\$B\$N\$h\$&\$J46\$8\$N5-=R\$K\$J\$kM=Dj\$G\$9e(B.

[ruby-dev:38392] e\$B\$+\$i\$NDs0F\$O\$\$\$m\$\$\$m\$HJQA+\$7\$F\$\$\$k\$N\$G\$9\$,!"e(B
e\$B8=;[email protected]\$G\$N7ABV\$Ne(B slice_by [email protected]\$H<!\$N\$h\$&\$K=q\$1\$^\$9!#e(B

a = %w[banana banana durian orange orange orange]
a.slice_by {|w| w }.each {|key, values| puts “#{key} #{values.size}” }

e\$B\$^\$!!"e(Bvalues e\$B\$,Bg\$-\$/\$J\$k\$H%a%b%[email protected]\$G\$OHa\$7\$\$%1!<%9\$,=P\$Fe(B
e\$B\$/\$k\$+\$bCN\$l\$^\$;\$s\$,!#e(B

``````word = nil
n = 0
``````

end
end

e\$B\$NMM\$K\$b=q\$1\$^\$9e(B. (1)e\$B\$,L#A9\$J\$s\$G\$9\$,e(B… (1)e\$B\$7\$J\$/\$F\$b\$A\$c\$s\$H=hM}\$7\$Fe(B
e\$B\$/\$l\$ke(Beach_with_peeke\$B8+\$?\$\$\$N\$,\$"\$C\$F\$b\$h\$\$\$+\$be(B?

e\$B:G=i\$+:G8e\$GFCJL07\$\$\$,I,MW\$J\$N\$G\$"\$s\$^\$j\$h\$/\$J\$\$\$H;W\$\$\$^\$9!#e(B

e\$B\$"\$H!"%3%s%H%m!<%k%V%l%\$%/\$r8!:w\$7\$?\$H\$3\$m!"\$I\$&\$d\$iJ#?t\$N%-!<e(B
e\$B\$r07\$&\$3\$H\$bDA\$7\$/\$J\$\$\$h\$&\$G\$9!#e(B

e\$B\$?\$H\$(\$P!"31\$5\$s\$NNc\$r\$A\$g\$C\$HJQ\$(\$F!"e(B

word1 docid1 linenum1
word1 docid1 linenum2
word1 docid2 linenum3
word1 docid2 linenum4
word2 docid1 linenum5
word2 docid1 linenum6
word2 docid2 linenum7
word2 docid2 linenum8

e\$B\$H\$\$\$&\$h\$&\$JJB\$S\$r=87W\$9\$k\$K\$O!"B?=E%k!<%W\$G=q\$-\$?\$\$\$o\$1\$G\$9!#e(B

e\$B\$?\$H\$(\$P!"0J2<\$N\$h\$&\$J46\$8\$G!#e(B

each_word {|word|

# word e\$BC10L\$N=87W\$N=i4|2=e(B

each_docid {|docid|
# docid e\$BC10L\$N=87W\$N=i4|2=e(B
each_linenum {|linenum|

}
# docid e\$BC10L\$N=87W\$N=*N;e(B
}

# word e\$BC10L\$N=87W\$N=*N;e(B

}

e\$B\$G\$b!"e(Beach_with_peek e\$B\$N\$h\$&\$J%\$%F%l!<%?\$G\$O\$3\$&\$\$\$&B?=E%k!<e(B
e\$B%W\$O<B8=\$G\$-\$^\$;\$s!#\$*\$=\$i\$/!#e(B

e\$B\$3\$3\$G!">e5-\$NB?=E%k!<%W\$O:F5"9_2<%Q!<%6\$H\$_\$J\$9\$3\$H\$b\$G\$-\$^e(B
e\$B\$9!#\$=\$N\$h\$&\$K9M\$(\$F%H!<%/%s\$N<[email protected]\$KAjEv\$9\$k\$H\$3\$m\$Ke(B
Enumerator#{peek,next} e\$B\$r;H\$&\$H!"\$3\$l\$O<B8=\$G\$-\$^\$9!#e(B

e\$B\$J\$<FbIt%\$%F%l!<%?\$G\$J\$/!"30It%\$%F%l!<%?\$r3HD%\$7\$F\$\$\$k\$N\$+!“e(B
e\$B\$H\$\$\$&M}M3\$O\$3\$3\$K\$”\$j\$^\$9!#e(B

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev:38935] the message: “[ruby-dev:38935] Re:
Enumerator#peek”, on Jul/29 00:29(JST) Tanaka A. writes:

e\$B\$"\$k%0%k!<%W\$KJ,\$1\$F=87W\$7\$?\$\$\$J\$ie(B, e\$B2<5-\$N\$h\$&\$JJ}\$,e(B Ruby e\$B\$]\$\$\$+\$be(B.
a = %w[banana banana durian orange orange orange]
a.group_by{|w| w}.each{|key, values| puts “#{key} #{values.size}”}
MapReducee\$B7O\$G\$O>e5-\$N\$h\$&\$J46\$8\$G=q\$/\$3\$H\$,B?\$\$\$H;W\$\$\$^\$9e(B.

group_by e\$B\$O;DG0\$J\$3\$H\$K\$<\$s\$V%a%b%j\$KFI\$_9~\$`\$3\$H\$K\$J\$k\$s\$Ge(B
e\$B\$9\$h\$M!#e(B

e\$B\$?\$7\$+\$Ke(B, Rubye\$B\$N\$O\$=\$&\$J\$C\$F\$\$\$^\$9\$h\$Me(B.

[email protected](B, e\$B%=!<%H:Q\$_\$rA0Ds\$H\$7\$J\$\$\$G=hM}\$G\$-\$k\$N\$Ge(B,
e\$B%=!<%H\$5\$l\$F\$J\$\$>uBV\$Ne(B
e\$B\$b\$N\$r\$o\$6\$o\$6%=!<%H\$7\$F%3%s%H%m!<%k%V%l!<%/=hM}\$9\$k\$N\$HJQ\$o\$i\$J\$\$\$+\$be(B
e\$BCN\$l\$^\$;\$se(B.

e\$B%=!<%H:Q\$_\$G\$"\$k\$3\$H\$rA0Ds\$H\$7\$F%7!<%1%s%7%c%k\$K=hM}\$G\$-\$k%ae(B
e\$B%=%C%I\$bEvA3\$"[email protected]\$F!"\$=\$l\$O\$=\$l\$G9M\$(\$F\$\$\$^\$9!#e(B
[ruby-dev:38392] e\$B\$+\$i;O\$^\$k%9%l%C%I\$G!"\$A\$g\$C\$H\$[\$C\$?\$i\$+\$7e(B
e\$B\$K\$7\$F\$\$\$k\$N\$G\$9\$,!“K:\$l\$F\$\$\$k\$o\$1\$G\$O\$”\$j\$^\$;\$s!#e(B

e\$B0JA0\$=\$s\$JOCBj\$,\$"\$j\$^\$7\$?\$Me(B. e\$B7k9=;29M\$K\$J\$k\$H\$3\$m\$,B?\$\$\$+\$be(B

e\$B\$=\$&\$\$\$&!"%7!<%1%s%7%c%k\$K=hM}\$9\$kA`:n\$NCf\$G!“e(Bpeek e\$B\$O\$+\$J\$je(B
e\$BDc5i\$J\$H\$3\$m\$r;Y1g\$9\$kOC\$K\$”\$?\$j\$^\$9!#e(B

e\$B\$J\$k\$[\$Ie(B.

[email protected]\$C\$F\$\$\$k\$H\$3\$m\$,0c\$&46\$8\$G\$7\$g\$&\$+!#e(B

e\$B\$&!<\$se(B.
Enumeratore\$B\$C\$F\$+\$J\$j1~MQE*\$J%/%i%9e(B([email protected]@.\$5\$l\$ke(B)
e\$B\$J\$N\$Ge(B, peeke\$B\$C\$F\$\$\$&\$N\$Oe(B, e\$B\$A\$g\$C\$H0cOB46\$,\$"\$k\$+\$be(B.

[ruby-dev:38392] e\$B\$+\$i\$NDs0F\$O\$\$\$m\$\$\$m\$HJQA+\$7\$F\$\$\$k\$N\$G\$9\$,!"e(B
e\$B8=;[email protected]\$G\$N7ABV\$Ne(B slice_by [email protected]\$H<!\$N\$h\$&\$K=q\$1\$^\$9!#e(B

a = %w[banana banana durian orange orange orange]
a.slice_by {|w| w }.each {|key, values| puts “#{key} #{values.size}”
}

e\$B%=!<%H\$5\$l\$F\$\$\$k\$H\$\$\$&A0Ds\$G\$G\$9\$Me(B. e\$B\$H\$\$\$&\$3\$H\$Oe(B,
slice_bye\$B\$5\$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$N\$h\$&\$K9M\$(\$F%H!<%/%s\$N<[email protected]\$KAjEv\$9\$k\$H\$3\$m\$Ke(B
Enumerator#{peek,next} e\$B\$r;H\$&\$H!"\$3\$l\$O<B8=\$G\$-\$^\$9!#e(B

e\$B\$?\$7\$+\$Ke(B. [email protected](B:

e\$B\$J\$<FbIt%\$%F%l!<%?\$G\$J\$/!"30It%\$%F%l!<%?\$r3HD%\$7\$F\$\$\$k\$N\$+!“e(B
e\$B\$H\$\$\$&M}M3\$O\$3\$3\$K\$”\$j\$^\$9!#e(B

each_with_peeke\$B\$be(BEnumeratore\$B\$N\$D\$b\$j\$J\$N\$G30It%\$%F%l!<%?\$K\$G\$-\$k\$H9M\$(\$Fe(B
e\$B\$\$\$^\$9e(B.
e\$B\$=\$l\$Ke(Bslice_bye\$B\$G\$be(BEnumeratore\$B\$H\$7\$F07\$(\$P<B8=2DG=\$J5\$\$,\$7\$^\$9\$,e(B?

peeke\$B\$G=PMh\$k\$3\$H\$,e(B,
slice_bye\$B\$NMM\$J\$b\$N\$G\$9\$Y\$F<B8=2DG=\$G\$O\$J\$\$\$+\$bCN\$le(B
e\$B\$^\$;\$s\$,e(B, slice_bye\$B\$\$?\$\$\$J\$b\$N\$NJ}\$,9%\$[email protected]\$J\$!e(B.

__
---------------------------------------------------->> [email protected](B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

In article [email protected],
[email protected] ([email protected]=<ye(B) writes:

[email protected](B, e\$B%=!<%H:Q\$_\$rA0Ds\$H\$7\$J\$\$\$G=hM}\$G\$-\$k\$N\$Ge(B, e\$B%=!<%H\$5\$l\$F\$J\$\$>uBV\$Ne(B
e\$B\$b\$N\$r\$o\$6\$o\$6%=!<%H\$7\$F%3%s%H%m!<%k%V%l!<%/=hM}\$9\$k\$N\$HJQ\$o\$i\$J\$\$\$+\$be(B
e\$BCN\$l\$^\$;\$se(B.

e\$B%a%=%C%I\$N;EMM\$H\$7\$F!"%=!<%H:Q\$_\$+\$I\$&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$I\$KBP\$9\$k30It%\$%F%l!<%?\$r<B8=\$9\$k\$N\$Oe(B
e\$B4JC1\$J\$N\$K!"e(BFiber e\$B\$r;H\$&\$N\$OL5BL\$,2a\$.\$^\$9!#e(B

e\$B\$?\$H\$(\$P!"e(Benum = io.lines e\$B\$H\$7\$?\$H\$-\$Ke(B enum.next
e\$B\$O!"7k6Ie(B
io.gets [email protected]\$1\$J\$N\$K!"e(BFiber
e\$B\$,;H\$o\$l\$k\$N\$O\$J\$s\$H\$bL5BL\$G\$9!#e(B

e\$B\$b\$7!"e(BEnumerator.external_iterator { … } e\$B\$H\$\$\$&\$h\$&\$J7A<0e(B
e\$B\$Ge(B enumerator e\$B\$,@8@.\$G\$-\$F!“e(Bnext
e\$B\$O;XDj\$7\$?%V%m%C%/[email protected](B
e\$B\$1!”\$H\$\$\$&\$N\$,<B8=\$G\$-\$l\$P!"e(B(e\$B;d\$N9=A[\$G\$Oe(B next
[email protected]\$_\$N=he(B
e\$BM}\$b\$7\$^\$9\$,e(B) io.lines e\$B\$Oe(B
Enumerator.external_iterator { io.gets or raise StopIteration }
e\$B\$G<B8=\$G\$-\$^\$9!#e(B

IO, Array, Range e\$BEy!“4pK\E*\$J%/%i%9\$Ne(B Enumerator e\$B\$O!”\$A\$g\$Ce(B
e\$B\$H\$,\$s\$P\$C\$F8zN(\$NNI\$\$30It%\$%F%l!<%?\$rDs6!\$7\$FNI\$\$\$N\$G\$O\$J\$\$e(B
e\$B\$+\$H;W\$C\$F\$\$\$^\$9!#e(B

e\$B%=!<%H\$5\$l\$F\$\$\$k\$H\$\$\$&A0Ds\$G\$G\$9\$Me(B. e\$B\$H\$\$\$&\$3\$H\$Oe(B, slice_bye\$B\$5\$(\$"\$l\$Pe(B,
e\$B%3%s%H%m!<%k%V%l!<%/=hM}\$G\$Oe(Bpeeke\$B\$OI,\$:[email protected]\$C\$F\$\$\$^\$;\$se(B?

e\$B\$3\$NNc\$G\$O\$=\$&\$G\$9\$M!#e(Bpeek e\$B\$,K\Ev\$KM-8z\$J\$N\$O\$h\$jJ#;(\$J%1!<e(B
e\$B%9\$G\$9!#%3%s%H%m!<%k%V%l!<%/\$G%V%l!<%/%-!<\$,J#?t\$"\$k\$H\$+!#e(B

e\$B=P\$7\$?Nc\$,E,@Z\$G\$O\$J\$+\$C\$?\$+\$bCN\$l\$^\$;\$s!#e(B

e\$B\$^\$!!"e(Bvalues e\$B\$,Bg\$-\$/\$J\$k\$H%a%b%[email protected]\$G\$OHa\$7\$\$%1!<%9\$,=P\$Fe(B
e\$B\$/\$k\$+\$bCN\$l\$^\$;\$s\$,!#e(B

e\$B\$?\$7\$+\$Ke(B, fairye\$B\$G\$be(Bgroup_bye\$B\$Ge(Bvaluese\$B\$rEO\$9\$N\$O\$I\$&[email protected]\$o\$l\$F\$\$\$^\$9e(B
(^^;;

e\$B\$=\$&\$\$\$(\$P!"e(B[ruby-dev:38934] e\$B\$N%3!<%I\$Ge(B

| a = %w[banana banana durian orange orange orange]
| a.inject_by(proc{|w| w}){|key, sum, value| sum += value}.each{|key,sum| puts “#{key} #{sum}”}

e\$B\$H\$\$\$&\$N\$O!"G[Ns\$r:n\$i\$J\$\$\$G:Q\$`\$H\$\$\$&[email protected][email protected]\$C\$F\$\$\$k\$H;W\$&\$Ne(B
e\$B\$G\$9\$,!“e(Bsum += value e\$B\$H\$\$\$&\$H\$3\$m\$,0UL#[email protected]\$G\$9!#%V%m%C%/JQe(B
e\$B?t\$Ne(B sum e\$B\$KBeF~\$7\$F\$I\$&[email protected]!”\$H\$+!“e(Bvalue
e\$B\$O\$I\$3\$+\$i=P\$F\$/e(B
e\$B\$k\$N\$+!”\$H\$+!#e(B

e\$B;W\$&\$K!“J#?t\$N%^%7%s\$KJ,;6\$5\$;\$F=hM}\$r\$d\$m\$&\$H\$\$\$&\$J\$i!“C18le(B
e\$B?t\$r?t\$(\$k\$H\$-\$NB-\$7;;\$N\$h\$&\$Ke(B associative e\$B\$J1i;;\$O!“e(B
associative e\$B\$G\$”\$k\$3\$H\$rMxMQ\$7\$?;E3]\$1\$G\$d\$C\$?\$[\$&\$,\$\$\$\$\$s\$8\$ce(B
e\$B\$J\$\$\$+\$J\$!!”\$H;W\$\$\$^\$9!#e(Bassociative e\$B\$G\$”\$l\$P!"\$D\$^\$j!“e(B
(a+b)+c=a+(b+c) e\$B\$N\$h\$&\$J4X78\$N1i;;\$G\$”\$l\$P!"3F%^%7%sFb\$G8D!9e(B
e\$B\$K7k2L\$r\$^\$H\$a\$i\$l\$^\$9\$+\$i!#e(B

inject [email protected]\$H!"@hF,\$+\$i=g\$K\$d\$C\$F\$\$\$+\$J\$1\$l\$P\$J\$i\$J\$\$46\$8\$,\$7e(B
e\$B\$^\$9!#\$D\$^\$j!“e(Bassociative e\$B\$G\$J\$\$1i;;\$rM?\$(\$F\$h\$\$!”\$H\$\$\$&46\$8e(B
e\$B\$,!#e(B

each_with_peeke\$B\$be(BEnumeratore\$B\$N\$D\$b\$j\$J\$N\$G30It%\$%F%l!<%?\$K\$G\$-\$k\$H9M\$(\$Fe(B
e\$B\$\$\$^\$9e(B. e\$B\$=\$l\$Ke(Bslice_bye\$B\$G\$be(BEnumeratore\$B\$H\$7\$F07\$(\$P<B8=2DG=\$J5\$\$,\$7\$^\$9\$,e(B?

each_with_peek e\$B\$re(B Enumerator e\$B\$K\$9\$k\$H\$\$\$&\$N\$O!"\$-\$C\$He(B Fiber
e\$B\$r;H\$&\$3\$H\$K\$J\$j\$^\$9\$h\$M!#\$=\$l\$O7y\$G\$9!#\$"\$H!"e(Beach_with_peek
e\$B\$O:[email protected]\$_\$r\$I\$&\$9\$k\$+\$,LdBj\$G\$9\$+\$M!#e(Bnil e\$B\$H\$9\$ke(B
e\$B\$H!“e(Bnil e\$B\$H\$\$\$&MWAG\$,\$”\$C\$?\$H\$-\$H6hJL\$G\$-\$^\$;\$s!#e(B

e\$B\$"\$H!“e(Bslice_by
e\$B\$OG[Ns\$K\$7\$A\$c\$\$\$^\$9\$+\$i!”%a%b%[email protected]\$G7y\$G\$9!#e(B

peeke\$B\$G=PMh\$k\$3\$H\$,e(B, slice_bye\$B\$NMM\$J\$b\$N\$G\$9\$Y\$F<B8=2DG=\$G\$O\$J\$\$\$+\$bCN\$le(B
e\$B\$^\$;\$s\$,e(B, slice_bye\$B\$\$?\$\$\$J\$b\$N\$NJ}\$,9%\$[email protected]\$J\$!e(B.

slice_by e\$B\$Oe(B slice_by e\$B\$G\$\$\$\$\$s\$8\$c\$J\$\$\$G\$7\$g\$&\$+!#e(B
slice_by e\$B\$G4JC1\$K\$G\$-\$k\$3\$H\$O!"e(Bslice_by e\$B\$G\$d\$k\$[\$&\$,\$\$\$\$\$H;We(B
e\$B\$\$\$^\$9!#e(B

peek e\$B\$O!"\$h\$jDc5i\$G\$h\$j1~MQHO0O\$,9-\$\$\$b\$N\$,[email protected]\$\$\$G\$9!#e(B

e\$B%3%s%H%m!<%k%V%l!<%/\$G%V%l!<%/%-!<\$,J#?t\$"\$k\$H\$\$\$&\$N\$O!"7k9=e(B
e\$B0lHLE*\$_\$?\$\$\$G\$9!#>pJs=hM}5;=Q<T;n83\$K=P\$F\$/\$k\$[\$I!#e(B

[email protected]\$3\$H\$O\$J\$\$\$N\$G3N\$+\$J\$o\$1\$8\$c\$J\$\$\$G\$9\$,!"!VFsCJ\$N%3%s%He(B
e\$B%m!<%k%V%l%\$%/=hM}!W\$J\$I\$HL<!\$K\$"\$kK\$b\$"\$k\$h\$&\$G\$9!#e(B
http://gihyo.jp/book/2002/4-7741-1607-6

peek e\$B\$O!"\$=\$&\$\$\$&\$H\$3\$m\$r;Y1g\$9\$k%Y!<%9\$K\$J\$k\$b\$N\$G\$9!#e(B

e\$B\$=\$N\$^\$^;H\$&\$H\$\$\$&\$h\$j\$OItIJ\$J\$s\$G\$9\$,!"@hFI\$_\$,I,MW\$J\$N\$Oe(B
([email protected]\$H9M\$(\$k\$He(B) e\$B\$+\$J\$j3N\$+\$J\$N\$G!"\$^\$:\$=\$3\$r\$D\$1\$k\$N\$Oe(B
e\$B\$I\$&\$+!"\$H\$\$\$&OC\$G\$9!#e(B

In article [email protected],
Urabe S. [email protected] writes:

[email protected]\$+\$i\$NN`[email protected]\$(\$Pe(BEnumerablee\$B\$Ke(Bungetce\$B\$N\$h\$&\$J\$b\$N\$,\$"\$l\$P\$\$\$\$\$s\$8\$c\$J\$\$e(B
e\$B\$G\$9\$+\$M\$(!#e(B

# [email protected]\$N;W\$\$\$D\$-\$J\$N\$G;[email protected]\$G\$9\$,e(B

ungetc e\$B\$C\$F!"[email protected]\$b\$N\$H0c\$&\$b\$N\$rLa\$;\$?\$j!“FI\$_9~\$^\$Je(B
e\$B\$/\$F\$bLa\$;\$?\$j!”%U%!%\$%k=*C<\$N07\$\$\$,Fq\$7\$+\$C\$?\$j!“7y\$8\$c\$”\$je(B
e\$B\$^\$;\$se(B?

e\$B\$I\$&\$;[email protected][email protected]\$1\$G!"\$=\$&\$\$\$&7y\$J\$H\$3\$m\$K\$D\$-\$"\$&M}e(B
e\$BM3\$O\$J\$\$!"\$H\$\$\$&\$N\$,;d\$NH=CG\$G\$9!#e(B

Tanaka A. e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

e\$B\$=\$N\$^\$^;H\$&\$H\$\$\$&\$h\$j\$OItIJ\$J\$s\$G\$9\$,!"@hFI\$_\$,I,MW\$J\$N\$Oe(B
([email protected]\$H9M\$(\$k\$He(B) e\$B\$+\$J\$j3N\$+\$J\$N\$G!"\$^\$:\$=\$3\$r\$D\$1\$k\$N\$Oe(B
e\$B\$I\$&\$+!"\$H\$\$\$&OC\$G\$9!#e(B

[email protected]\$+\$i\$NN`[email protected]\$(\$Pe(BEnumerablee\$B\$Ke(Bungetce\$B\$N\$h\$&\$J\$b\$N\$,\$"\$l\$P\$\$\$\$\$s\$8\$c\$J\$\$e(B
e\$B\$G\$9\$+\$M\$(!#e(B

# [email protected]\$N;W\$\$\$D\$-\$J\$N\$G;[email protected]\$G\$9\$,e(B

e\$B\$3\$s\$K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\$P!"e(Benum = io.lines e\$B\$H\$7\$?\$H\$-\$Ke(B enum.next e\$B\$O!"7k6Ie(B
io.gets [email protected]\$1\$J\$N\$K!"e(BFiber e\$B\$,;H\$o\$l\$k\$N\$O\$J\$s\$H\$bL5BL\$G\$9!#e(B

e\$B\$G\$9\$h\$M\$'e(B…

e\$B\$b\$7!"e(BEnumerator.external_iterator { … } e\$B\$H\$\$\$&\$h\$&\$J7A<0e(B
e\$B\$Ge(B enumerator e\$B\$,@8@.\$G\$-\$F!“e(Bnext e\$B\$O;XDj\$7\$?%V%m%C%/[email protected](B
e\$B\$1!”\$H\$\$\$&\$N\$,<B8=\$G\$-\$l\$P!"e(B(e\$B;d\$N9=A[\$G\$Oe(B next [email protected]\$_\$N=he(B
e\$BM}\$b\$7\$^\$9\$,e(B) io.lines e\$B\$Oe(B
Enumerator.external_iterator { io.gets or raise StopIteration }
e\$B\$G<B8=\$G\$-\$^\$9!#e(B

getse\$B\$H\$+\$J\$ie(B, e\$B\$=\$l\$G9T\$1\$=\$&\$G\$9\$Me(B.

IO, Array, Range e\$BEy!“4pK\E*\$J%/%i%9\$Ne(B Enumerator e\$B\$O!”\$A\$g\$Ce(B
e\$B\$H\$,\$s\$P\$C\$F8zN(\$NNI\$\$30It%\$%F%l!<%?\$rDs6!\$7\$FNI\$\$\$N\$G\$O\$J\$\$e(B
e\$B\$+\$H;W\$C\$F\$\$\$^\$9!#e(B

Arraye\$B\$J\$s\$+\$Oe(B, e\$B>e5-\$Ne(B Enumerator.external_iterator
e\$B\$_\$?\$\$\$Je(B, e\$B\$d\$jJ}\$Ge(B
e\$B\$O<B8=\$G\$-\$=\$&\$b\$J\$\$e(B(fibere\$B;H\$(\$PJL\$G\$9\$,e(B(^^;;)e\$B\$N\$GJLESBP=h\$,I,MW\$G\$9\$Me(B.

e\$B\$=\$&\$\$\$(\$P!"e(B[ruby-dev:38934] e\$B\$N%3!<%I\$Ge(B

| a = %w[banana banana durian orange orange orange]
| a.inject_by(proc{|w| w}){|key, sum, value| sum += value}.each{|key,sum| puts “#{key} #{sum}”}

e\$B\$H\$\$\$&\$N\$O!"G[Ns\$r:n\$i\$J\$\$\$G:Q\$`\$H\$\$\$&[email protected][email protected]\$C\$F\$\$\$k\$H;W\$&\$Ne(B
e\$B\$G\$9\$,!“e(Bsum += value e\$B\$H\$\$\$&\$H\$3\$m\$,0UL#[email protected]\$G\$9!#%V%m%C%/JQe(B
e\$B?t\$Ne(B sum e\$B\$KBeF~\$7\$F\$I\$&[email protected]!”\$H\$+!“e(Bvalue e\$B\$O\$I\$3\$+\$i=P\$F\$/e(B
e\$B\$k\$N\$+!”\$H\$+!#e(B

e\$B\$4\$a\$s\$J\$5\$\$e(B. e\$B\$3\$N>l9g\$Oe(B

a.inject_by(proc{|w| w}){|key, sum, value| sum += 1}.each{|key,sum| puts
“#{key} #{sum}”}

e\$B\$G\$7\$?e(B. e\$B\$3\$N>l9ge(Bvaluee\$B\$K\$Oe(B w e\$B<+?H\$,EO\$k\$N\$G\$9\$,e(B.
e\$B%o!<%[email protected]\$1%+%&%s%H\$9e(B
e\$B\$k\$N\$Ge(B1e\$BB-\$;\$P\$h\$\$\$3\$H\$K\$J\$j\$^\$9e(B.

e\$B;W\$&\$K!“J#?t\$N%^%7%s\$KJ,;6\$5\$;\$F=hM}\$r\$d\$m\$&\$H\$\$\$&\$J\$i!“C18le(B
e\$B?t\$r?t\$(\$k\$H\$-\$NB-\$7;;\$N\$h\$&\$Ke(B associative e\$B\$J1i;;\$O!“e(B
associative e\$B\$G\$”\$k\$3\$H\$rMxMQ\$7\$?;E3]\$1\$G\$d\$C\$?\$[\$&\$,\$\$\$\$\$s\$8\$ce(B
e\$B\$J\$\$\$+\$J\$!!”\$H;W\$\$\$^\$9!#e(Bassociative e\$B\$G\$”\$l\$P!"\$D\$^\$j!“e(B
(a+b)+c=a+(b+c) e\$B\$N\$h\$&\$J4X78\$N1i;;\$G\$”\$l\$P!"3F%^%7%sFb\$G8D!9e(B
e\$B\$K7k2L\$r\$^\$H\$a\$i\$l\$^\$9\$+\$i!#e(B

e\$B<B:]\$O\$=\$N\$h\$&\$K=hM}\$7\$^\$9e(B. e\$B\$^\$:e(B,
e\$B%m!<%+%k\$Ge(Binject(reduce)e\$B\$7\$Fe(B, e\$B3F%N!<e(B
e\$B%I\$N\$b\$N\$r\$^\$H\$a\$Fe(Binject(reduce)e\$B\$7\$^\$9e(B. e\$B\$G\$9\$N\$Ge(B,
e\$B7k9gN’\$OA0Ds\$K\$J\$C\$Fe(B
e\$B\$\$\$^\$9e(B.

inject [email protected]\$H!"@hF,\$+\$i=g\$K\$d\$C\$F\$\$\$+\$J\$1\$l\$P\$J\$i\$J\$\$46\$8\$,\$7e(B
e\$B\$^\$9!#\$D\$^\$j!“e(Bassociative e\$B\$G\$J\$\$1i;;\$rM?\$(\$F\$h\$\$!”\$H\$\$\$&46\$8e(B
e\$B\$,!#e(B

e\$B\$"!<e(B. inject e\$B\$C\$F\$=\$s\$J%\$%a!<%8\$"\$j\$^\$9e(B? e\$B\$\$\$A\$*\$&e(B,
reducee\$B\$H\$\$\$&L>A0\$be(B
aliase\$B\$7\$F\$\$\$^\$9\$,e(B,
Rubye\$B\$Ne(Binjecte\$B\$H;w\$F\$\$\$k\$N\$GF1\$8L>A0\$K\$7\$^\$7\$?e(B.

each_with_peek e\$B\$re(B Enumerator e\$B\$K\$9\$k\$H\$\$\$&\$N\$O!"\$-\$C\$He(B Fiber
e\$B\$r;H\$&\$3\$H\$K\$J\$j\$^\$9\$h\$M!#\$=\$l\$O7y\$G\$9!#e(B

e\$B\$=\$l\$Oe(B, e\$BF10U\$7\$^\$9e(B(^^;;;

e\$B\$"\$H!"e(Beach_with_peek
e\$B\$O:[email protected]\$_\$r\$I\$&\$9\$k\$+\$,LdBj\$G\$9\$+\$M!#e(Bnil e\$B\$H\$9\$ke(B
e\$B\$H!“e(Bnil e\$B\$H\$\$\$&MWAG\$,\$”\$C\$?\$H\$-\$H6hJL\$G\$-\$^\$;\$s!#e(B

e\$B3N\$+\$Ke(B, e\$B\$=\$l\$O\$"\$C\$Fe(B, e\$BG[Ns\$G<u\$1\$?>l9g\$Oe(B,
e\$BMWAG?t8:\$i\$9\$N\$+\$J\$!\$H\$+9M\$(e(B
e\$B\$?\$N\$G\$9\$,e(B… e\$B\$"\$^\$j\$-\$l\$\$\$K=hM}\$G\$-\$J\$\$5\$\$,\$7\$F\$\$\$^\$9e(B.

e\$B\$"\$H!“e(Bslice_by e\$B\$OG[Ns\$K\$7\$A\$c\$\$\$^\$9\$+\$i!”%a%b%[email protected]\$G7y\$G\$9!#e(B

a = %w[banana banana durian orange orange orange]
a.slice_by {|w| w }.each {|key, values| puts “#{key} #{values.size}” }

e\$B\$Ne(B values e\$B\$N\$3\$H\$G\$9e(B?

slice_by e\$B\$Oe(B slice_by e\$B\$G\$\$\$\$\$s\$8\$c\$J\$\$\$G\$7\$g\$&\$+!#e(B
slice_by e\$B\$G4JC1\$K\$G\$-\$k\$3\$H\$O!"e(Bslice_by e\$B\$G\$d\$k\$[\$&\$,\$\$\$\$\$H;We(B
e\$B\$\$\$^\$9!#e(B

peek e\$B\$O!"\$h\$jDc5i\$G\$h\$j1~MQHO0O\$,9-\$\$\$b\$N\$,[email protected]\$\$\$G\$9!#e(B

e\$BDc5i\$J\$N\$OG’\$a\$^\$9e(B(^^; e\$B1~MQHO0O\$O\$^[email protected](B?e\$B\$G\$9\$,e(B…

e\$B%3%s%H%m!<%k%V%l!<%/\$G%V%l!<%/%-!<\$,J#?t\$"\$k\$H\$\$\$&\$N\$O!"7k9=e(B
e\$B0lHLE*\$_\$?\$\$\$G\$9!#>pJs=hM}5;=Q<T;n83\$K=P\$F\$/\$k\$[\$I!#e(B

[email protected]\$3\$H\$O\$J\$\$\$N\$G3N\$+\$J\$o\$1\$8\$c\$J\$\$\$G\$9\$,!"!VFsCJ\$N%3%s%He(B
e\$B%m!<%k%V%l%\$%/=hM}!W\$J\$I\$HL<!\$K\$"\$kK\$b\$"\$k\$h\$&\$G\$9!#e(B
http://gihyo.jp/book/2002/4-7741-1607-6

e\$B\$3\$l\$K4X\$7\$F\$G\$9\$,e(B,

slice_bye\$B\$Ge(B
valuese\$B\$,Mh\$?\$i\$=\$l\$r:#EY\$OBhe(B2e\$BMWAG\$Ge(Bslice_bye\$B\$9\$l\$P\$h\$\$\$h\$&\$Je(B
e\$B5\$\$,\$7\$^\$9e(B.

a = [[word1 docid1 linenum1],
[word1 docid1 linenum2],…

a.slice_by{|l| l[0]}.each do |key, values|
values.slice_by{|l| l[1]}.each do |key1, values1|

2e\$BCJ\$N%3%s%H%m!<%k%V%l!<%/=hM}\$r4*0c\$\$\$7\$F\$\$\$J\$1\$l\$P\$G\$9\$,e(B.

[email protected]\$+e(B,
slice_bye\$B\$O7k9=;H\$(\$k\$+\$b\$C\$F5\$\$K\$J\$C\$F\$-\$^\$7\$?e(B(^^;;;
e\$BG[Ns\$O\$I\$s\$I\$s=PMh\$F\$7\$^\$\$\$^\$9\$1\$I\$Me(B(^^;

peek e\$B\$O!"\$=\$&\$\$\$&\$H\$3\$m\$r;Y1g\$9\$k%Y!<%9\$K\$J\$k\$b\$N\$G\$9!#e(B

slice_bye\$B\$re(B Ruby(e\$B\$N%9%/%j%W%He(B)e\$B\$G<B8=\$7\$?\$1\$l\$Pe(B
peeke\$B\$b\$7\$/\$Oe(Bpeeke\$B\$HF1\$8=he(B
e\$BM}\$,I,MW\$K\$J\$j\$^\$9\$Me(B. —(1)

e\$B\$=\$N\$^\$^;H\$&\$H\$\$\$&\$h\$j\$OItIJ\$J\$s\$G\$9\$,!"@hFI\$_\$,I,MW\$J\$N\$Oe(B
([email protected]\$H9M\$(\$k\$He(B) e\$B\$+\$J\$j3N\$+\$J\$N\$G!"\$^\$:\$=\$3\$r\$D\$1\$k\$N\$Oe(B
e\$B\$I\$&\$+!"\$H\$\$\$&OC\$G\$9!#e(B

e\$B\$?\$7\$+\$Ke(B, (1)e\$B\$N\$h\$&\$J\$3\$H\$,<B8=\$G\$-\$k\$N\$Ge(B peek
e\$B\$,\$"\$k\$H\$\$\$m\$\$\$mJXMx\$Je(B
e\$B5\$\$,\$7\$F\$-\$^\$7\$?e(B.

e\$B\$=\$l\$Ke(B e\$B<BAu\$be(B,
e\$B\$3\$l\$K\$h\$C\$F<B9T%3%9%H\$,L\$K8+\$(\$FA}\$(\$k\$C\$F\$o\$1\$G\$b\$Je(B
e\$B\$\$\$G\$9\$7\$Me(B.

__
---------------------------------------------------->> [email protected](B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

In article [email protected],
[email protected] ([email protected]=<ye(B) writes:

fairye\$B\$G\$Oe(B, e\$BJ,;6%=!<%H\$NFbIt=hM}\$G%^!<%8%=!<%H\$r;H\$C\$F\$\$\$k\$N\$G\$9\$,e(B, e\$B\$=e(B
e\$B\$&\$\$\$&\$N\$,\$"\$k\$H\$+\$J\$jJXMx\$G\$9e(B(^^;;

e\$B%=!<%H:Q\$_\$N\$rJ#?t%^!<%8\$9\$k\$H\$\$\$&\$N\$O!“FbIt%\$%F%l!<%?\$G\$O\$Ge(B
e\$B\$-\$J\$\$\$N\$G!”\$d\$C\$Q\$j30It%\$%F%l!<%?\$r;H\$&\$3\$H\$K\$J\$j\$^\$9!#e(B

e\$B\$3\$N>l9ge(B peek e\$B\$Oe(B…
e\$B%^!<%8\$NESCf\$GCfCG\$7\$?\$H\$-\$KMWAG\$,>xH/\$7e(B
e\$B\$F\$7\$^\$o\$J\$\$\$h\$&\$K\$9\$k\$N\$KMxMQ\$G\$-\$^\$9\$M!#\$^\$!!"\$9\$4\$/Lr\$KN)e(B
e\$B\$D\$H\$\$\$&\$[\$I\$G\$O\$"\$j\$^\$;\$s\$,!#e(B

e\$B\$"\$H!“e(B2e\$B8D\$8\$c\$J\$/\$F0lHL\$Ke(B
ne\$B8D\$r%^!<%8!”\$H\$+9M\$(\$k\$H!"\$^\$:M%e(B
[email protected]=g0L\$D\$-%-%e!<\$,M_\$7\$\$\$G\$9\$h\$M\$’!#\$J\$s\$GI8=`E:IU\$5\$l\$F\$\$\$Je(B
[email protected]\$m\$&!#e(B

e\$B\$?\$,\$ke(Bqueuee\$B8+\$?\$\$\$N\$b<B8=\$G\$-\$J\$\$\$G\$9\$7e(B…

e\$B\$b\$”\$j\$^\$9\$M!#e(B

IO, Array, Range e\$BEy!“4pK\E*\$J%/%i%9\$Ne(B Enumerator e\$B\$O!”\$A\$g\$Ce(B
e\$B\$H\$,\$s\$P\$C\$F8zN(\$NNI\$\$30It%\$%F%l!<%?\$rDs6!\$7\$FNI\$\$\$N\$G\$O\$J\$\$e(B
e\$B\$+\$H;W\$C\$F\$\$\$^\$9!#e(B

Arraye\$B\$J\$s\$+\$Oe(B, e\$B>e5-\$Ne(B Enumerator.external_iterator e\$B\$_\$?\$\$\$Je(B, e\$B\$d\$jJ}\$Ge(B
e\$B\$O<B8=\$G\$-\$=\$&\$b\$J\$\$e(B(fibere\$B;H\$(\$PJL\$G\$9\$,e(B(^^;;)e\$B\$N\$GJLESBP=h\$,I,MW\$G\$9\$Me(B.

e\$B\$?\$H\$(\$PG[Ns\$J\$i!"e(B

i = -1
Enumerator.external_iterator {
i += 1
raise StopIteration if ary.length <= i
ary[i]
}

e\$B\$H\$\$\$&\$h\$&\$K\$G\$-\$^\$9!#e(B

e\$B\$^\$!!“e(Brewind e\$B\$de(B marshal e\$B\$r\$I\$&\$9\$k\$+\$H\$+!“e(Beach
[email protected]\$i\$Ie(B
e\$B\$&\$9\$k\$+\$H\$+!”\$”\$^\$j40A4\$H\$O\$\$\$(\$J\$\$\$N\$b;v<B\$G\$O\$"\$k\$N\$G\$9e(B
e\$B\$,!"\$&\$^\$/\$\$\$/%G%6%\$%[email protected]\$m\$&!"\$H;W\$C\$F\$\$\$^\$9!#e(B

e\$B\$K7k2L\$r\$^\$H\$a\$i\$l\$^\$9\$+\$i!#e(B

e\$B<B:]\$O\$=\$N\$h\$&\$K=hM}\$7\$^\$9e(B. e\$B\$^\$:e(B, e\$B%m!<%+%k\$Ge(Binject(reduce)e\$B\$7\$Fe(B, e\$B3F%N!<e(B
e\$B%I\$N\$b\$N\$r\$^\$H\$a\$Fe(Binject(reduce)e\$B\$7\$^\$9e(B. e\$B\$G\$9\$N\$Ge(B, e\$B7k9gN’\$OA0Ds\$K\$J\$C\$Fe(B
e\$B\$\$\$^\$9e(B.

e\$B\$(!<\$H!"%V%m%C%/\$NCf\$re(B sum += 1 e\$B\$H\$7\$F\$7\$^\$C\$?\$N\$G!“e(B
inject_by e\$B\$O\$=\$&\$O\$G\$-\$J\$/\$J\$C\$F\$\$\$k!”\$H;W\$&\$s\$G\$9\$,\$I\$&\$G\$7\$ge(B
e\$B\$&!#e(B

sum += 1 e\$B\$H\$\$\$&%V%m%C%/\$r;H\$C\$Fe(B sum1 + sum2 e\$B\$H\$\$\$&B-\$7;;\$r9Te(B
e\$B\$&\$N\$OL5M}\$C\$]\$\$\$G\$9\$h\$M!#e(B

e\$B\$"!<e(B. inject e\$B\$C\$F\$=\$s\$J%\$%a!<%8\$"\$j\$^\$9e(B? e\$B\$\$\$A\$*\$&e(B, reducee\$B\$H\$\$\$&L>A0\$be(B
aliase\$B\$7\$F\$\$\$^\$9\$,e(B, Rubye\$B\$Ne(Binjecte\$B\$H;w\$F\$\$\$k\$N\$GF1\$8L>A0\$K\$7\$^\$7\$?e(B.

inject e\$B\$H\$\$\$&L>A0\$Oe(B Ruby e\$B\$G\$7\$+7P83\$,\$J\$\$\$N\$G!"JP\$C\$F\$k\$+\$be(B
e\$B\$7\$l\$^\$;\$s!#e(B

e\$B\$"\$H!“e(Bslice_by e\$B\$OG[Ns\$K\$7\$A\$c\$\$\$^\$9\$+\$i!”%a%b%[email protected]\$G7y\$G\$9!#e(B

a = %w[banana banana durian orange orange orange]
a.slice_by {|w| w }.each {|key, values| puts “#{key} #{values.size}” }

e\$B\$Ne(B values e\$B\$N\$3\$H\$G\$9e(B?

e\$B\$O\$\$!#e(B

2e\$BCJ\$N%3%s%H%m!<%k%V%l!<%/=hM}\$r4*0c\$\$\$7\$F\$\$\$J\$1\$l\$P\$G\$9\$,e(B.

[email protected]\$+e(B, slice_bye\$B\$O7k9=;H\$(\$k\$+\$b\$C\$F5\$\$K\$J\$C\$F\$-\$^\$7\$?e(B(^^;;;
e\$BG[Ns\$O\$I\$s\$I\$s=PMh\$F\$7\$^\$\$\$^\$9\$1\$I\$Me(B(^^;

e\$BG[Ns\$r:n\$C\$F\$7\$^\$C\$FNI\$1\$l\$P!"\$G\$-\$^\$9!#e(B

peek e\$B\$N0U?^\$O!"\$=\$l\$rHr\$1\$?\$\$\$H\$\$\$&OC\$G\$9!#e(B

slice_bye\$B\$re(B Ruby(e\$B\$N%9%/%j%W%He(B)e\$B\$G<B8=\$7\$?\$1\$l\$Pe(B peeke\$B\$b\$7\$/\$Oe(Bpeeke\$B\$HF1\$8=he(B
e\$BM}\$,I,MW\$K\$J\$j\$^\$9\$Me(B. —(1)

e\$B\$\$\$d!“e(Bslice_by e\$B\$OFbIt%\$%F%l!<%?\$G<BAu\$7\$^\$9!#30It%\$%F%l!<%?e(B
e\$B\$O;H\$\$\$^\$;\$s\$N\$G!“e(Bpeek e\$B\$b;H\$\$\$^\$;\$s!#FbIt%\$%F%l!<%?\$r;H\$&\$He(B
[email protected]\$_\$r9T\$&e(B
(e\$B\$H\$\$\$&\$+!”=hM}\$r0l=dCY1d\$5\$;\$ke(B)e\$B!”\$H\$\$\$&e(B
e\$B0UL#\$G\$7\$?\$i\$=\$N\$H\$*\$j\$G\$9\$,!#e(B

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

In message “Re: [ruby-dev:38932] Enumerator#peek”
on Tue, 28 Jul 2009 18:45:00 +0900, Tanaka A. [email protected]
writes:

|Enumerator#peek [email protected]_\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$"\$C\$FNI\$\$\$H;W\$\$\$^\$9!#e(B1.9.2e\$B\$KF~\$l\$F\$h\$\$\$+\$Oe(BYuguie\$B\$5\$s\$N>5Bz\$re(B
[email protected]\$kI,MW\$,\$"\$j\$^\$9\$,!"\$?\$V\$sNI\$\$\$H\$*\$C\$7\$c\$k\$N\$G\$O\$J\$\$\$+\$H!#e(B

In article [email protected],
Yukihiro M. [email protected] writes:

|Enumerator#peek [email protected]_\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$"\$C\$FNI\$\$\$H;W\$\$\$^\$9!#e(B1.9.2e\$B\$KF~\$l\$F\$h\$\$\$+\$Oe(BYuguie\$B\$5\$s\$N>5Bz\$re(B
[email protected]\$kI,MW\$,\$"\$j\$^\$9\$,!"\$?\$V\$sNI\$\$\$H\$*\$C\$7\$c\$k\$N\$G\$O\$J\$\$\$+\$H!#e(B

yugui e\$B\$5\$s!"\$I\$&\$G\$7\$g\$&e(B?

e\$B\$1\$\$\$8\$e!w\$\$\$7\$D\$+\$G\$9e(B.

In [ruby-dev:38973] the message: “[ruby-dev:38973] Re:
Enumerator#peek”, on Aug/01 01:54(JST) Tanaka A. writes:

e\$B%=!<%H:Q\$_\$N\$rJ#?t%^!<%8\$9\$k\$H\$\$\$&\$N\$O!“FbIt%\$%F%l!<%?\$G\$O\$Ge(B
e\$B\$-\$J\$\$\$N\$G!”\$d\$C\$Q\$j30It%\$%F%l!<%?\$r;H\$&\$3\$H\$K\$J\$j\$^\$9!#e(B

e\$B3N\$+\$KFbIt=hM}\$O\$=\$&\$J\$C\$F\$\$\$^\$9e(B.

e\$B\$3\$N>l9ge(B peek e\$B\$Oe(B… e\$B%^!<%8\$NESCf\$GCfCG\$7\$?\$H\$-\$KMWAG\$,>xH/\$7e(B
e\$B\$F\$7\$^\$o\$J\$\$\$h\$&\$K\$9\$k\$N\$KMxMQ\$G\$-\$^\$9\$M!#\$^\$!!"\$9\$4\$/Lr\$KN)e(B
e\$B\$D\$H\$\$\$&\$[\$I\$G\$O\$"\$j\$^\$;\$s\$,!#e(B

e\$B\$=\$l\$G\$be(B, ne\$B8D\$N%^!<%[email protected]\$H%"%k%4%j%:%`\$,7k9=\$-\$l\$\$\$K\$J\$j\$^\$9e(B.
[email protected][email protected]\$se(B,
peeke\$B\$,M_\$7\$/\$J\$C\$F\$-\$?e(B(^^;;

e\$B\$"\$H!“e(B2e\$B8D\$8\$c\$J\$/\$F0lHL\$Ke(B ne\$B8D\$r%^!<%8!”\$H\$+9M\$(\$k\$H!"\$^\$:M%e(B
[email protected]=g0L\$D\$-%-%e!<\$,M_\$7\$\$\$G\$9\$h\$M\$’!#\$J\$s\$GI8=`E:IU\$5\$l\$F\$\$\$Je(B
[email protected]\$m\$&!#e(B

e\$B\$3\$l\$bM_\$7\$\$\$+\$be(B(^^;;

e\$B\$?\$,\$ke(Bqueuee\$B8+\$?\$\$\$N\$b<B8=\$G\$-\$J\$\$\$G\$9\$7e(B…

e\$B\$b\$”\$j\$^\$9\$M!#e(B

e\$B30It%\$%F%l!<%?\$HFbIt%\$%F%l!<%?\$N0c\$\$\$O%V%m%C%/\$N6-3&\$r1[\$(\$i\$l\$k\$+\$I\$&e(B
e\$B\$+\$J\$s\$G\$9\$,e(B,
e\$B:#\$Ne(BEnumeratore\$B\$N;[email protected]\$H%a%=%C%IFb\$G6I=jE*\$K;H\$&\$J\$ie(B(e\$B%9e(B
e\$B%l%C%I\$rGD0.\$G\$-\$k\$N\$Ge(B)e\$B\$^[email protected]\$h\$\$\$G\$9\$,e(B,
e\$B%a%=%C%I\$r\$^\$?\$,\$C\$?\$j\$9\$k\$H;He(B
e\$B\$(\$J\$\$46\$8\$J\$s\$G\$9\$h\$M\$'e(B…

}

e\$B\$H\$\$\$&\$h\$&\$K\$G\$-\$^\$9!#e(B

e\$B\$*!<e(B. e\$B5\$\$,IU\$+\$J\$+\$C\$?e(B…
e\$B\$“e(B, e\$B\$G\$be(B,
e\$B\$3\$l\$OG[Ns\$,%\$%s%G%C%/%9%”%/%;%[email protected]\$+\$i\$G\$9\$Me(B. hashe\$B\$OL5M}e(B
e\$B\$+\$be(B… e\$B\$G\$be(B,
1.9e\$B\$Ne(BHashe\$B\$OFbIt\$G=g=x\$r0];}\$7\$F\$\$\$k\$+\$i%\$%s%G%C%/%9%"%/e(B
e\$B%;%9\$r5v\$9\$h\$&\$K\$9\$l\$P2DG=\$J5\$\$,\$7\$^\$9\$Me(B.

e\$B\$^\$!!“e(Brewind e\$B\$de(B marshal e\$B\$r\$I\$&\$9\$k\$+\$H\$+!“e(Beach [email protected]\$i\$Ie(B
e\$B\$&\$9\$k\$+\$H\$+!”\$”\$^\$j40A4\$H\$O\$\$\$(\$J\$\$\$N\$b;v<B\$G\$O\$"\$k\$N\$G\$9e(B
e\$B\$,!"\$&\$^\$/\$\$\$/%G%6%\$%[email protected]\$m\$&!"\$H;W\$C\$F\$\$\$^\$9!#e(B

e\$B%\$%s%G%C%/%9%"%/%;%92DG=\$J\$i\$PBg>fIW\$J5\$\$,\$7\$^\$9e(B.
e\$B\$=\$&\$9\$k\$He(B,
e\$B\$+\$J\$j\$Ne(BEnumerablee\$B\$JAH\$9~\$%/%i%9\$N4X\$7\$F\$Oe(BEnumeratore\$B\$K\$Ge(B
e\$B\$-\$=\$&\$G\$9\$Me(B.

a.inject_by(proc{|w| w}){|key, sum, value| sum += 1}.each{|key,sum| puts “#{key} #{sum}”}
e\$B\$(!<\$H!"%V%m%C%/\$NCf\$re(B sum += 1 e\$B\$H\$7\$F\$7\$^\$C\$?\$N\$G!“e(B
inject_by e\$B\$O\$=\$&\$O\$G\$-\$J\$/\$J\$C\$F\$\$\$k!”\$H;W\$&\$s\$G\$9\$,\$I\$&\$G\$7\$ge(B
e\$B\$&!#e(B

sum += 1 e\$B\$H\$\$\$&%V%m%C%/\$r;H\$C\$Fe(B sum1 + sum2 e\$B\$H\$\$\$&B-\$7;;\$r9Te(B
e\$B\$&\$N\$OL5M}\$C\$]\$\$\$G\$9\$h\$M!#e(B

e\$B\$J\$+\$J\$+1T\$\$\$G\$9\$Me(B(^^;;;
e\$B\$^\$?4V0c\$(\$F\$7\$^\$\$\$^\$7\$?e(B. e\$B\$3\$N>l9ge(B:

a.inject_by(proc{|w| w}){|key, sum, value| sum + 1}.each{|key,sum| puts
“#{key} #{sum}”}

e\$B\$G\$7\$?e(B. e\$B\$"e(B. e\$B\$G\$be(B,
[email protected]\$H7k9gN’@.\$jN)\$?\$J\$\$\$G\$9\$7e(B, e\$B%0%k!<%WKh\$N>.7W\$He(B
e\$B\$+\$b\$H\$l\$J\$\$\$s\$G\$9\$Me(B… [email protected]\$He(B, key,
valuee\$B\$N6hJL\$,\$J\$\$\$N\$G\$9\$,e(B,e\$BAmOBe(B
e\$B\$r\$H\$C\$?\$j\$9\$k\$K\$Oe(Bvaluee\$BIt\$r<h\$j=P\$;\$J\$\$\$H\$^\$:\$\$\$J\$!e(B…
e\$B\$3\$s\$J46\$8\$K\$Je(B
e\$B\$j\$^\$9\$+e(B…

a.inject_by(proc{|w| w}, :value => proc{|w| 1}){|key, sum, value| sum +
value}.each{|key,sum| puts “#{key} #{sum}”}

e\$B\$"!<e(B. inject e\$B\$C\$F\$=\$s\$J%\$%a!<%8\$"\$j\$^\$9e(B? e\$B\$\$\$A\$*\$&e(B, reducee\$B\$H\$\$\$&L>A0\$be(B
aliase\$B\$7\$F\$\$\$^\$9\$,e(B, Rubye\$B\$Ne(Binjecte\$B\$H;w\$F\$\$\$k\$N\$GF1\$8L>A0\$K\$7\$^\$7\$?e(B.

inject e\$B\$H\$\$\$&L>A0\$Oe(B Ruby e\$B\$G\$7\$+7P83\$,\$J\$\$\$N\$G!"JP\$C\$F\$k\$+\$be(B
e\$B\$7\$l\$^\$;\$s!#e(B

inject e\$B\$C\$Fe(B [email protected]\$H;W\$\$\$^\$9\$,e(B,
e\$B?tCM\$N\$J\$i\$S\$N4V\$K1i;;e(B
e\$B;R\$rCf\$K\$\$\$l\$k\$C\$F\$H\$3\$m\$+\$iMh\$F\$\$\$k\$N\$+\$J!)e(B e\$B\$H9M\$(\$F\$\$\$^\$9e(B.
e\$B\$G\$9\$N\$Ge(B,
e\$B\$=\$l\$K7k9gN’\$rA0Ds\$H\$7\$F\$b\$^\$"\$h\$\$\$s\$8\$c\$J\$\$\$+\$H9M\$(\$?\$o\$1\$G\$9e(B.

[email protected]\$+e(B, slice_bye\$B\$O7k9=;H\$(\$k\$+\$b\$C\$F5\$\$K\$J\$C\$F\$-\$^\$7\$?e(B(^^;;;
e\$BG[Ns\$O\$I\$s\$I\$s=PMh\$F\$7\$^\$\$\$^\$9\$1\$I\$Me(B(^^;
e\$BG[Ns\$r:n\$C\$F\$7\$^\$C\$FNI\$1\$l\$P!"\$G\$-\$^\$9!#e(B

peek e\$B\$N0U?^\$O!"\$=\$l\$rHr\$1\$?\$\$\$H\$\$\$&OC\$G\$9!#e(B

e\$B\$J\$k\$[\$Ie(B.

slice_bye\$B\$re(B Ruby(e\$B\$N%9%/%j%W%He(B)e\$B\$G<B8=\$7\$?\$1\$l\$Pe(B peeke\$B\$b\$7\$/\$Oe(Bpeeke\$B\$HF1\$8=he(B
e\$BM}\$,I,MW\$K\$J\$j\$^\$9\$Me(B. —(1)

e\$B\$\$\$d!“e(Bslice_by e\$B\$OFbIt%\$%F%l!<%?\$G<BAu\$7\$^\$9!#30It%\$%F%l!<%?e(B
e\$B\$O;H\$\$\$^\$;\$s\$N\$G!“e(Bpeek e\$B\$b;H\$\$\$^\$;\$s!#FbIt%\$%F%l!<%?\$r;H\$&\$He(B
[email protected]\$_\$r9T\$&e(B (e\$B\$H\$\$\$&\$+!”=hM}\$r0l=dCY1d\$5\$;\$ke(B)e\$B!”\$H\$\$\$&e(B
e\$B0UL#\$G\$7\$?\$i\$=\$N\$H\$*\$j\$G\$9\$,!#e(B

e\$B\$=\$l\$,e(B, e\$B8eH>\$Ne(Bpeeke\$B\$HF1\$8=hM}\$N0UL#\$N\$D\$b\$j\$G\$7\$?e(B.

__
---------------------------------------------------->> [email protected](B
e\$B7=<ye(B <<—
---------------------------------->> e-mail: [email protected] <<—

In article [email protected],
Tanaka A. [email protected] writes:

|Enumerator#peek [email protected]_\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B

e\$B\$"\$C\$FNI\$\$\$H;W\$\$\$^\$9!#e(B1.9.2e\$B\$KF~\$l\$F\$h\$\$\$+\$Oe(BYuguie\$B\$5\$s\$N>5Bz\$re(B
[email protected]\$kI,MW\$,\$"\$j\$^\$9\$,!"\$?\$V\$sNI\$\$\$H\$*\$C\$7\$c\$k\$N\$G\$O\$J\$\$\$+\$H!#e(B

yugui e\$B\$5\$s!"\$I\$&\$G\$7\$g\$&e(B?

e\$B\$7\$P\$i\$/BT\$C\$F\$\$?\$N\$G\$9\$,!"H?1~\$,\$J\$\$\$N\$Ge(B trunk
e\$B\$KF~\$l\$F\$
\$^\$7\$?!#e(B
e\$BLdBj\$,\$"\$k\$J\$i\$\$\$C\$F\$/[email protected]\$5\$\$!#e(B

In article [email protected],
[email protected] ([email protected]=<ye(B) writes:

e\$B\$*!<e(B. e\$B5\$\$,IU\$+\$J\$+\$C\$?e(B…
e\$B\$“e(B, e\$B\$G\$be(B, e\$B\$3\$l\$OG[Ns\$,%\$%s%G%C%/%9%”%/%;%[email protected]\$+\$i\$G\$9\$Me(B. hashe\$B\$OL5M}e(B
e\$B\$+\$be(B… e\$B\$G\$be(B, 1.9e\$B\$Ne(BHashe\$B\$OFbIt\$G=g=x\$r0];}\$7\$F\$\$\$k\$+\$i%\$%s%G%C%/%9%"%/e(B
e\$B%;%9\$r5v\$9\$h\$&\$K\$9\$l\$P2DG=\$J5\$\$,\$7\$^\$9\$Me(B.

e\$B\$"\$!!"e(BHash e\$B\$O9M\$(\$F\$^\$;\$s\$G\$7\$?!#;H\$&\$+\$J\$!e(B?

e\$B\$G\$7\$?e(B. e\$B\$"e(B. e\$B\$G\$be(B, [email protected]\$H7k9gN’@.\$jN)\$?\$J\$\$\$G\$9\$7e(B, e\$B%0%k!<%WKh\$N>.7W\$He(B
e\$B\$+\$b\$H\$l\$J\$\$\$s\$G\$9\$Me(B… [email protected]\$He(B, key, valuee\$B\$N6hJL\$,\$J\$\$\$N\$G\$9\$,e(B,e\$BAmOBe(B
e\$B\$r\$H\$C\$?\$j\$9\$k\$K\$Oe(Bvaluee\$BIt\$r<h\$j=P\$;\$J\$\$\$H\$^\$:\$\$\$J\$!e(B… e\$B\$3\$s\$J46\$8\$K\$Je(B
e\$B\$j\$^\$9\$+e(B…

a.inject_by(proc{|w| w}, :value => proc{|w| 1}){|key, sum, value| sum + value}.each{|key,sum| puts “#{key} #{sum}”}

e\$B\$1\$C\$-\$g\$/!"e(B

• [email protected]\$NMWAG\$+\$iJ,N`\$N\$?\$a\$NCM\$KJQ49\$9\$k\$b\$Ne(B
• [email protected]\$NMWAG\$+\$iFs9`1i;;\$KM?\$(\$kCM\$KJQ49\$9\$k\$b\$Ne(B
• e\$BFs9`1i;;e(B
e\$B\$H\$\$\$&e(B 3e\$B\$D\$N4X?t\$r;XDj\$7\$J\$\$\$H\$\$\$1\$^\$;\$s\$h\$M!#e(B

Smalltalk e\$B\$J\$i0z?t\$H\$7\$F%V%m%C%/\$rM?\$(\$k\$s\$G\$7\$g\$&\$,!"e(BRuby
[email protected]\$H\$J\$!!#e(Bgather_each e\$B\$G\$be(B proc
e\$B\$r0z?t\$KM?\$(\$k\$N\$O7y\$o\$l\$?\$7!#e(B