* enumerator.c (inspect_enumerator): use VALUE instead of mere char*
  by using rb_sprintf() and rb_id2str().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-05-18 02:05:17 +00:00
Родитель 6b6ca88925
Коммит 8a28e97ae9
3 изменённых файлов: 25 добавлений и 17 удалений

Просмотреть файл

@ -1,4 +1,7 @@
Sat May 18 11:03:05 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
Sat May 18 11:05:14 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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().

Просмотреть файл

@ -880,19 +880,18 @@ static VALUE
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 dummy, int recur)
}
/* (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 default_method, VALUE default_args)
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 default_method, VALUE default_args)
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);
}

Просмотреть файл

@ -391,6 +391,14 @@ class TestEnumerator < Test::Unit::TestCase
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 }