ruby-changes:28753
From: nobu <ko1@a...>
Date: Sat, 18 May 2013 11:05:28 +0900 (JST)
Subject: [ruby-changes:28753] nobu:r40805 (trunk): enumerator.c: use VALUE
nobu 2013-05-18 11:05:17 +0900 (Sat, 18 May 2013) New Revision: 40805 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40805 Log: enumerator.c: use VALUE * enumerator.c (inspect_enumerator): use VALUE instead of mere char* by using rb_sprintf() and rb_id2str(). Modified files: trunk/ChangeLog trunk/enumerator.c trunk/test/ruby/test_enumerator.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40804) +++ ChangeLog (revision 40805) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Sat May 18 11:03:05 2013 Nobuyoshi Nakada <nobu@r...> +Sat May 18 11:05:14 2013 Nobuyoshi Nakada <nobu@r...> + + * enumerator.c (inspect_enumerator): use VALUE instead of mere char* + by using rb_sprintf() and rb_id2str(). * enumerator.c (append_method): extract from inspect_enumerator(). Index: enumerator.c =================================================================== --- enumerator.c (revision 40804) +++ enumerator.c (revision 40805) @@ -880,19 +880,18 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enumerator.c#L880 inspect_enumerator(VALUE obj, VALUE dummy, int recur) { struct enumerator *e; - const char *cname; - VALUE eobj, str; + VALUE eobj, str, cname; TypedData_Get_Struct(obj, struct enumerator, &enumerator_data_type, e); - cname = rb_obj_classname(obj); + cname = rb_obj_class(obj); if (!e || e->obj == Qundef) { - return rb_sprintf("#<%s: uninitialized>", cname); + return rb_sprintf("#<%"PRIsVALUE": uninitialized>", rb_class_path(cname)); } if (recur) { - str = rb_sprintf("#<%s: ...>", cname); + str = rb_sprintf("#<%"PRIsVALUE": ...>", rb_class_path(cname)); OBJ_TAINT(str); return str; } @@ -903,9 +902,7 @@ inspect_enumerator(VALUE obj, VALUE dumm https://github.com/ruby/ruby/blob/trunk/enumerator.c#L902 } /* (1..100).each_cons(2) => "#<Enumerator: 1..100:each_cons(2)>" */ - str = rb_sprintf("#<%s: ", cname); - rb_str_append(str, rb_inspect(eobj)); - OBJ_INFECT(str, eobj); + str = rb_sprintf("#<%"PRIsVALUE": %+"PRIsVALUE, rb_class_path(cname), eobj); append_method(obj, str, e->meth, e->args); rb_str_buf_cat2(str, ">"); @@ -919,14 +916,14 @@ append_method(VALUE obj, VALUE str, ID d https://github.com/ruby/ruby/blob/trunk/enumerator.c#L916 VALUE method, eargs; method = rb_attr_get(obj, id_method); - if (NIL_P(method)) { + if (method != Qfalse) { + ID mid = default_method; + if (!NIL_P(method)) { + Check_Type(method, T_SYMBOL); + mid = SYM2ID(method); + } rb_str_buf_cat2(str, ":"); - rb_str_buf_cat2(str, rb_id2name(default_method)); - } - else if (method != Qfalse) { - Check_Type(method, T_SYMBOL); - rb_str_buf_cat2(str, ":"); - rb_str_buf_cat2(str, rb_id2name(SYM2ID(method))); + rb_str_buf_append(str, rb_id2str(mid)); } eargs = rb_attr_get(obj, id_arguments); @@ -943,7 +940,7 @@ append_method(VALUE obj, VALUE str, ID d https://github.com/ruby/ruby/blob/trunk/enumerator.c#L940 while (argc--) { VALUE arg = *argv++; - rb_str_concat(str, rb_inspect(arg)); + rb_str_append(str, rb_inspect(arg)); rb_str_buf_cat2(str, argc > 0 ? ", " : ")"); OBJ_INFECT(str, arg); } Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 40804) +++ test/ruby/test_enumerator.rb (revision 40805) @@ -391,6 +391,14 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L391 assert_warning("", bug6214) { [].lazy.inspect } end + def test_inspect_encoding + c = Class.new{define_method("\u{3042}"){}} + e = c.new.enum_for("\u{3042}") + s = assert_nothing_raised(Encoding::CompatibilityError) {break e.inspect} + assert_equal(Encoding::UTF_8, s.encoding) + assert_match(/\A#<Enumerator: .*:\u{3042}>\z/, s) + end + def test_generator # note: Enumerator::Generator is a class just for internal g = Enumerator::Generator.new {|y| y << 1 << 2 << 3; :foo } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/