diff options
| author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-17 12:10:30 +0000 |
|---|---|---|
| committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-17 12:10:30 +0000 |
| commit | 6764400540594bdeea8f64c0939a8b200bb97911 (patch) | |
| tree | 3caba8feb8af6ce7174ea1404e6be1af3459f209 | |
| parent | eca3dd75c53af87ce765b900d9f908400e8e2319 (diff) | |
* enumerator.c (inspect_enumerator): Implement #inspect.
[ruby-dev:37248]-[ruby-dev:37263]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@20851 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ChangeLog | 5 | ||||
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | enumerator.c | 67 |
3 files changed, 76 insertions, 0 deletions
@@ -1,3 +1,8 @@ +Wed Dec 17 21:02:55 2008 Akinori MUSHA <knu@iDaemons.org> + + * enumerator.c (inspect_enumerator): Implement #inspect. + [ruby-dev:37248]-[ruby-dev:37263] + Wed Dec 17 20:06:32 2008 Akinori MUSHA <knu@iDaemons.org> * lib/date.rb (once): Use Object#object_id instead of Symbol#to_i; @@ -40,6 +40,10 @@ with all sufficient information, see the ChangeLog file. Now calls the "rewind" method of the enclosed object if defined. + * Enumerator#inspect + + Implemented. + * Hash#default_proc= New method. diff --git a/enumerator.c b/enumerator.c index 3dc2ab9bbc..f33dbb7e12 100644 --- a/enumerator.c +++ b/enumerator.c @@ -575,6 +575,72 @@ enumerator_rewind(obj) } } +static VALUE +inspect_enumerator(obj, dummy, recur) + VALUE obj, dummy; + int recur; +{ + struct enumerator *e = enumerator_ptr(obj); + const char *cname = rb_obj_classname(obj); + VALUE eobj, str; + int tainted; + + if (recur) { + str = rb_str_buf_new2("#<"); + rb_str_buf_cat2(str, cname); + rb_str_buf_cat2(str, ": ...>"); + OBJ_TAINT(str); + return str; + } + + eobj = e->obj; + + tainted = OBJ_TAINTED(eobj); + + /* (1..100).each_cons(2) => "#<Enumerator: 1..100:each_cons(2)>" */ + str = rb_str_buf_new2("#<"); + rb_str_buf_cat2(str, cname); + rb_str_buf_cat2(str, ": "); + 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; + } + } + + rb_str_buf_cat2(str, ">"); + + if (tainted) OBJ_TAINT(str); + return str; +} + +/* + * call-seq: + * e.inspect => string + * + * Create a printable version of <i>e</i>. + */ + +static VALUE +enumerator_inspect(obj) + VALUE obj; +{ + return rb_exec_recursive(inspect_enumerator, obj, 0); +} + /* * Yielder */ @@ -836,6 +902,7 @@ Init_Enumerator() 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); |
