e$B1sF#$G$9!#e(B
Enumerator e$B$NCf?H$r$o$+$j$d$9$/I=<($7$F$/$l$ke(B Enumerator#inspect
e$B$re(B
e$BDs6!$9$k$HJXMx$@$H;W$$$^$9!#Nc$($P$3$s$J46$8!#e(B
$ ruby19 -e ‘g = (1…1000).each; p g’
#<Enumerator:[1, 2, 3, …(snipped)]>
e$B8=>u$Oe(B #Enumerator:0x8269654
e$B$H$+$J$N$G!"%G%P%C%0$K$OITJX$G$9!#e(B
e$B0J2<$O$?$?$-Bf$N%Q%C%A$G$9!#e(B
Index: enumerator.c
— enumerator.c (revision 20448)
+++ enumerator.c (working copy)
@@ -545,7 +545,69 @@
return obj;
}
+static VALUE
+inspect_enumerator_i(VALUE val, VALUE *memo)
+{
- VALUE str = memo[0];
- if (memo[1] >= 3) {
- rb_str_buf_cat2(str, “, …(snipped)”);
- rb_iter_break();
- }
- if (memo[1] > 0) rb_str_buf_cat2(str, ", ");
- rb_str_buf_append(str, rb_inspect(val));
- memo[1]++;
- if (OBJ_TAINTED(val)) memo[2] = Qtrue;
- if (OBJ_UNTRUSTED(val)) memo[3] = Qtrue;
- return Qnil;
+}
-
+static VALUE
+inspect_enumerator(VALUE obj, VALUE dummy, int recur)
+{
/*
-
-
- e.inspect => “#<Enumerator:[1, 2, 3, …(snipped)]>”
-
-
- Create a printable version of e.
- */
-
+static VALUE
+enumerator_inspect(VALUE obj)
+{
- return rb_exec_recursive(inspect_enumerator, obj, 0);
+}
-
+/*
- Yielder
*/
static void
@@ -779,6 +841,7 @@
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, “rewind”, enumerator_rewind, 0);
-
rb_define_method(rb_cEnumerator, “inspect”, enumerator_inspect, 0);
rb_eStopIteration = rb_define_class(“StopIteration”,
rb_eIndexError);
Index: lib/pp.rb
— lib/pp.rb (revision 20448)
+++ lib/pp.rb (working copy)
@@ -339,6 +339,27 @@
end
end
+class Enumerator
- def pretty_print(q)
- s = self.class.to_s
- q.group(s.size + 3, ‘#<’ + s + ‘:’, ‘]>’) {
-
idx = 0
-
q.seplist(take(4)) {|v|
-
if idx < 3
-
q.pp v
-
else
-
q.text "...(snipped)"
-
end
-
idx += 1
-
}
- }
- end
-
- def pretty_print_cycle(q)
- q.text(’#<’ + self.class.to_s + ‘:[…]’)
- end
+end
-
class Hash
def pretty_print(q)
q.pp_hash self
At Tue, 2 Dec 2008 23:59:52 +0900,
Yusuke ENDOH wrote:
Enumerator ã®ä¸èº«ã‚’ã‚ã‹ã‚Šã‚„ã™ã表示ã—ã¦ãれる Enumerator#inspect ã‚’
æä¾›ã™ã‚‹ã¨ä¾¿åˆ©ã ã¨æ€ã„ã¾ã™ã€‚例ãˆã°ã“ã‚“ãªæ„Ÿã˜ã€‚
$ ruby19 -e ‘g = (1…1000).each; p g’
#<Enumerator:[1, 2, 3, …(snipped)]>
ç¾çŠ¶ã¯ #Enumerator:0x8269654 ã¨ã‹ãªã®ã§ã€ãƒ‡ãƒãƒƒã‚°ã«ã¯ä¸ä¾¿ã§ã™ã€‚
 inspect ã§å®Ÿéš›ã«å›žã—ã¦ã—ã¾ã†ã®ã¯(ä¸èº«ã‚’見るãŸã‚ã®)副作用ã¨ã—ã¦
大ãã™ãŽã€é©åˆ‡ã§ã¯ãªã„æ°—ãŒã—ã¾ã™ã€‚
 Enumerable ã ã¨åˆ†ã‹ã£ã¦ã„るオブジェクトã®é ã®æ–¹ãŒè¦‹ãŸã„ã¨ã„ã†
ã ã‘ãªã‚‰
ruby19 -e ‘g = (1…1000).each; p g.take(3)’
ãらã„ã§ã„ã„ã¨æ€ã„ã¾ã™ãŒã€æƒ³å®šã™ã‚‹ã®ã¯ã©ã†ã„ã†ä½¿ã„é“ã§ã—ょã†ã‹ã€‚
 ãªãŠã€ Enumerator ãŒå†…包ã™ã‚‹ã‚ªãƒ–ジェクトã¸ã®ã‚¢ã‚¯ã‚»ã‚¹æ–¹æ³•ã‚’æä¾›
ã—ã¦ã„ãªã„ã®ã¯æ„図的ã§ã™ã€‚
e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B
In message “Re: [ruby-dev:37259] Re: [Feature:1.9] Enumerator#inspect”
on Wed, 3 Dec 2008 17:32:59 +0900, “Akinori MUSHA”
[email protected] writes:
|> Enumerator e$B$NCf?H$r$o$+$j$d$9$/I=<($7$F$/$l$ke(B Enumerator#inspect e$B$re(B
|> e$BDs6!$9$k$HJXMx$@$H;W$$$^$9!#Nc$($P$3$s$J46$8!#e(B
|>
|> $ ruby19 -e ‘g = (1…1000).each; p g’
|> #<Enumerator:[1, 2, 3, …(snipped)]>
|>
|> e$B8=>u$Oe(B #Enumerator:0x8269654 e$B$H$+$J$N$G!"%G%P%C%0$K$OITJX$G$9!#e(B
|
|e$B!!e(Binspect e$B$G<B:]$K2s$7$F$7$^$&$N$Oe(B(e$BCf?H$r8+$k$?$a$Ne(B)e$BI{:nMQ$H$7$Fe(B
|e$BBg$-$9$.!"E,@Z$G$O$J$$5$$,$7$^$9!#e(B
e$B$=$&$G$9$M$(!#e(BIOe$B$N$h$&$K:F3+2DG=$G$J$$e(BEnumerablee$B$b$“$j$^$9$7e(B
e$B$M$(!#;d$b$”$^$j;?@.$G$-$^$;$s!#$o$+$j$d$9$/$9$k$H$$$&%“%$%G%#e(B
e$B%”$K$OH?BP$G$O$J$$$s$G$9$,!#e(B
Enumeratore$B$NCf$K:G=i$Ne(B3e$BMWAG$r<h$C$F$*$/$H$+$O!"$?$V$s$d$j$9$.e(B
e$B$G$7$g$&$M$(!#e(B
e$B$3$s$K$A$O!"$J$+$`$ie(B(e$B$&e(B)e$B$G$9!#e(B
In message “[ruby-dev:37261] Re: [Feature:1.9] Enumerator#inspect”
on Dec.03,2008 18:17:53, [email protected] wrote:
e$B$=$&$G$9$M$(!#e(BIOe$B$N$h$&$K:F3+2DG=$G$J$$e(BEnumerablee$B$b$“$j$^$9$7e(B
e$B$M$(!#;d$b$”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C$H0z$C$+$+$j$^$9$,!”:Y$+$$$3$H$G$9!#e(B
e$B40A4$KK~B-$7$?$N$G!"0J2<$O<XB-$G$9$,!#e(B
e$BI{:nMQ$N$“$k%a%=%C%I$+$ie(B Enumerator
e$B$r:n$C$FJQ$J$3$H$K$J$k$N$O!”<+8J@UG$e(B
e$B$@$H;W$C$F$$$^$7$?!#e(B
e$B$J$<$+$H$$$&$H!"e(BIO#each e$B$+$i:n$C$?e(B Enumerator e$B$re(B
rewind e$B$7$F$bL5;k$5$l$ke(B
e$B$H$$$&e(B (e$B;d$K$H$C$F$Oe(B) e$BIT2D2r$J5sF0$r$9$k$?$a$G$9!#e(B
$ ruby19 -rstringio -e ’
s = StringIO.new(“foo\nbar\n”).each
p s.next
s.rewind
p s.next
’
“foo\n”
“bar\n”
e$B$3$l$+$i2?$r46$8<h$C$?$+$H$$$&$He(B
- rewind e$B$O<B:]$K$Oe(B rewind
e$B$;$:!“<o$N%a%=%C%I$r8F$SD>$9$H$$$&0UL#$G$”$ke(B
- e$B$=$l$K$b4X$o$i$:e(B rewind e$B$H$$$&L>A0$,IU$1$i$l$F$$$ke(B
- Enumerator
e$B$N<o$H$J$k%a%=%C%I$O8F$SD>$9$H:G=i$+$iNs5s$7;O$a$k$H$$$&e(B
e$B@-<Ae(B (e$B$D$^$je(B Array#each e$B$N$h$&$J$Ne(B)
e$B$,4|BT$5$l$F$$$ke(B
- e$B$=$l<+BN$,I{:nMQ$r;}$D$h$&$J%a%=%C%I$Ge(B Enumerator
e$B$r:n$k$N$O<+8J@UG$e(B
e$B$H$$$&J70O5$$G$7$?!#<XB-$G$7$?!#e(B
At Wed, 3 Dec 2008 18:39:59 +0900,
U.Nakamura wrote:
| inspect ã§å®Ÿéš›ã«å›žã—ã¦ã—ã¾ã†ã®ã¯(ä¸èº«ã‚’見るãŸã‚ã®)副作用ã¨ã—ã¦
EnumeratorオブジェクトãŒã©ã“ã‹ã‚‰å‡ºã¦ããŸã®ã‹ãŒã‚ã‹ã‚‹æ–¹ãŒå¬‰ã—
ã„よã†ãªæ°—ãŒã—ã¾ã™ã€‚
上記ã®ä¾‹ã ã¨ã€ä¾‹ãˆã°
#Enumerator:Range#each
ã¨ã‹ã€‚
ã¾ã‚ã€ãƒ‡ãƒãƒƒã‚°ç”¨ã ã‹ã‚‰è¦‹ã›ã¦ã—ã¾ã£ã¦ã‚‚ã„ã„ã‹ã‚‚ã—れã¾ã›ã‚“ã。
(1…100).each_cons(2) => “#<Enumerator: 1…100:each_cons(2)>”
ã¨ã„ã†æ„Ÿã˜ã§ã©ã†ã§ã—ょã†ã‹ã€‚(ä¸‹ã«æ·»ä»˜)
ã§ã‚‚ã€ã©ã‚“ãªå‡ºæ–¹ãŒå¬‰ã—ã„ã‹ã¯ã€ãƒ‡ãƒãƒƒã‚°ã®ã‚„り方ã«ã‚ˆã‚‹ã‚“ã§ã—ょ
ã†ããˆã€‚
ãªãŠã€ç§ã¯å‹•ã„ã¦ã‚‹ã‚¹ã‚¯ãƒªãƒ—トã®ä¸ã«pを何個ã‹ã°ã‚‰ã¾ã„ã¦æŒ™å‹•を追
ã£ãŸã‚Šã™ã‚‹ã®ã§ã€pã®æœ‰ç„¡ã§(stdoutã¸ã®å‡ºåЛ以外ã«)動作ãŒå¤‰åŒ–ã—ã¦
ã—ã¾ã†å¯èƒ½æ€§ãŒã‚ã‚‹ã®ã¯ã‚ã‚“ã¾ã‚Šå¬‰ã—ããªã„ã§ã™ã€‚
ç§ã‚‚ãã†æ€ã„ã¾ã™ã€‚
Index: enumerator.c
— enumerator.c (revision 20453)
+++ enumerator.c (working copy)
@@ -545,6 +545,68 @@ enumerator_rewind(VALUE obj)
return obj;
}
+static VALUE
+inspect_enumerator(VALUE obj, VALUE dummy, int recur)
+{
- struct enumerator *e = enumerator_ptr(obj);
- const char *cname = rb_obj_classname(obj);
- VALUE eobj, str;
- int tainted, untrusted;
-
- if (recur) {
- str = rb_sprintf("#<%s: …>", cname);
- OBJ_TAINT(str);
- return str;
- }
-
- eobj = e->obj;
-
- tainted = OBJ_TAINTED(eobj);
- untrusted = OBJ_UNTRUSTED(eobj);
-
- /* (1…100).each_cons(2) => “#<Enumerator: 1…100:each_cons(2)>” */
- str = rb_sprintf("#<%s: ", cname);
- rb_str_concat(str, rb_inspect(eobj));
- rb_str_buf_cat2(str, “:”);
- rb_str_buf_cat2(str, rb_id2name(e->meth));
-
- if (e->args) {
- int argc = RARRAY_LEN(e->args);
- VALUE *argv = RARRAY_PTR(e->args);
-
- rb_str_buf_cat2(str, “(”);
-
- while (argc–) {
-
VALUE arg = *argv++;
-
-
rb_str_concat(str, rb_inspect(arg));
-
rb_str_buf_cat2(str, argc > 0 ? ", " : ")");
-
-
if (OBJ_TAINTED(arg)) tainted = Qtrue;
-
if (OBJ_UNTRUSTED(arg)) untrusted = Qtrue;
- }
- }
-
- rb_str_buf_cat2(str, “>”);
-
- if (tainted) OBJ_TAINT(str);
- if (untrusted) OBJ_UNTRUST(str);
- return str;
+}
-
+/*
-
-
-
-
- Create a printable version of e.
- */
-
+static VALUE
+enumerator_inspect(VALUE obj)
+{
- return rb_exec_recursive(inspect_enumerator, obj, 0);
+}
-
/*
- Yielder
*/
@@ -779,6 +841,7 @@ Init_Enumerator(void)
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, “rewind”, enumerator_rewind, 0);
-
rb_define_method(rb_cEnumerator, “inspect”, enumerator_inspect, 0);
rb_eStopIteration = rb_define_class(“StopIteration”,
rb_eIndexError);
e$B%A%1%C%He(B #810 e$B$,99?7$5$l$^$7$?!#e(B (by Akinori MUSHA)
e$B%9%F!<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B
e$B?JD=e(B % 0e$B$+$ie(B100e$B$KJQ99e(B
Applied in changeset r20481.
http://redmine.ruby-lang.org/issues/show/810
At Wed, 3 Dec 2008 20:36:41 +0900,
Yusuke ENDOH wrote:
2008/12/03 19:57 Akinori MUSHA [email protected]:
(snip)
ã¾ã‚ã€ãƒ‡ãƒãƒƒã‚°ç”¨ã ã‹ã‚‰è¦‹ã›ã¦ã—ã¾ã£ã¦ã‚‚ã„ã„ã‹ã‚‚ã—れã¾ã›ã‚“ã。
(1…100).each_cons(2) => “#<Enumerator: 1…100:each_cons(2)>”
ã¨ã„ã†æ„Ÿã˜ã§ã©ã†ã§ã—ょã†ã‹ã€‚(ä¸‹ã«æ·»ä»˜)
ãŠãŠã€è¦ã¯ã€ŒEnumerator ã®æ£ä½“ã‚’ p ã ã‘ã§ (ã‚る程度) 知りãŸã„ã€ã¨ã„ã†
ã“ã¨ãªã®ã§ã€ã“れã§å分ã§ã™ã€‚
. ã§ã¯ãªã : ãªã®ãŒã¡ã‚‡ã£ã¨å¼•ã£ã‹ã‹ã‚Šã¾ã™ãŒã€ç´°ã‹ã„ã“ã¨ã§ã™ã€‚
 . ã ã¨Rangeãªã©ã®å ´åˆç‰¹ã« Ruby ã®å¼ã£ã½ã見ãˆã‚‹ã‘れã©ã‚‚ã€
ã¨ã„ã†æ„Ÿã˜ãŒã—ãŸã®ã§ : ã«ã—ã¾ã—ãŸã€‚
s.rewind
性質 (ã¤ã¾ã‚Š Array#each ã®ã‚ˆã†ãªã®) ãŒæœŸå¾…ã•れã¦ã„ã‚‹
- ãれ自体ãŒå‰¯ä½œç”¨ã‚’æŒã¤ã‚ˆã†ãªãƒ¡ã‚½ãƒƒãƒ‰ã§ Enumerator を作るã®ã¯è‡ªå·±è²¬ä»»
ã¨ã„ã†é›°å›²æ°—ã§ã—ãŸã€‚蛇足ã§ã—ãŸã€‚
ãã†ã§ã™ã。IOç³»ã®ã‚ˆã†ã«ã€ã‚ªãƒ–ジェクト内部ã«èµ°æŸ»ä¸ã®ä½ç½®ãŒä¿æŒ
ã•れã¦ã„ã‚‹ã‚‚ã®ã¯ rewind ã§ãã¦ã„ã¾ã›ã‚“ã。1.8 㨠1.9 ã§æŒ™å‹•ãŒ
ç•°ãªã£ã¦ã„ã‚‹ã®ã‚‚ã¾ãšã„ã‹ã‚‚。
オブジェクト㮠respond_to?(:rewind) ãŒçœŸã ã£ãŸã‚‰ rewind を呼ã¶
よã†ã«ã™ã¹ããªã®ã‹ãªã€‚
e$B%A%1%C%He(B #818 e$B$,99?7$5$l$^$7$?!#e(B (by Akinori MUSHA)
e$B%9%F!<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B
Committed on trunk.
http://redmine.ruby-lang.org/issues/show/818
At Thu, 4 Dec 2008 11:28:45 +0900,
I wrote:
At Wed, 3 Dec 2008 20:36:41 +0900,
Yusuke ENDOH wrote:
(snip)
ç•°ãªã£ã¦ã„ã‚‹ã®ã‚‚ã¾ãšã„ã‹ã‚‚。
オブジェクト㮠respond_to?(:rewind) ãŒçœŸã ã£ãŸã‚‰ rewind を呼ã¶
よã†ã«ã™ã¹ããªã®ã‹ãªã€‚
 ãã‚“ãªæ°—ãŒã—ãŸã®ã§ã€ trunk ã§ã¯ãã®ã‚ˆã†ã«ã—ã¾ã—ãŸã€‚
At Sat, 13 Dec 2008 00:05:00 +0900,
Yugui (Yuki S.) wrote:
ãã ã¨æ€ã„ã¾ã™ã‹?
 IO系を enumeratorize ã—ãŸå ´åˆã« rewind ㌠rewind ã—ã¦ãれãªã„
ã¨ã„ã†æ„味ã§ã¯æ„図ã›ã–る挙動ã ã£ãŸã¨è¨€ãˆãªãã‚‚ãªã„ã§ã™ã。ã¾ãŸã€
rewind ã—ã¦ã‚‚æ„味ãŒãªã‹ã£ãŸã®ã§ãã®æŒ™å‹•ã«ç©æ¥µçš„ã«æœŸå¾…ã™ã‚‹ã“ã¨ã¯
ãªã„ã¨æŽ¨æ¸¬ã•れã€ã‚ã¾ã‚Šäº’æ›æ€§ä¸Šã®å•題ã«ã¯ãªã‚‰ãªã„æ°—ãŒã—ã¾ã™ã€‚
 rewind 自体ãŒãŠãらãレアãªã®ã§ã€å޳坆ã«ã¯éžäº’æ›ã¨ã¯ã„ãˆã€ç§ã¯
マージã—ã¦ã„ã„ã¨æ€ã„ã¾ã™ã€‚
Yuguie$B$G$9!#e(B
Akinori MUSHA e$B$5$s$O=q$-$^$7$?e(B:
e$B$=$&$G$9$M!#e(BIOe$B7O$N$h$&$K!"%*%V%8%’%/%HFbIt$KAv::Cf$N0LCV$,J];}e(B
e$B$5$l$F$$$k$b$N$Oe(B rewind e$B$G$-$F$$$^$;$s$M!#e(B1.8 e$B$He(B 1.9 e$B$G5sF0$,e(B
e$B0[$J$C$F$$$k$N$b$^$:$$$+$b!#e(B
e$B%*%V%8%’%/%H$Ne(B respond_to?(:rewind) e$B$,??$@$C$?$ie(B rewind e$B$r8F$Ve(B
e$B$h$&$K$9$Y$-$J$N$+$J!#e(B
e$B%P!<%8%g%s4V0l4S@-$G$$$&$H!“e(B1.9.1e$B$He(B1.9.2e$B$Ge(Brewinde$B$N5sF0$,0c$&$N$bG:$^$7$$e(B
e$B$H;W$$$^$9!#7k9=Bg$-$J;EMMJQ99$@$H;W$&$s$G$9$,!”$3$l!"e(B1.9.1e$B$K<h$j9~$`$Ye(B
e$B$-$@$H;W$$$^$9$+e(B?