Skip to content

Commit

Permalink
* hash.c (Init_Hash): made #to_s an alias to #inspect to reduce
Browse files Browse the repository at this point in the history
  the result of recursive array.  a patch from ujihisa at
  [ruby-core:23601].  [ruby-dev:38555]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23605 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed May 29, 2009
1 parent 893349c commit 2a6fb96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
7 changes: 4 additions & 3 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Fri May 29 10:04:32 2009 Nobuyoshi Nakada <[email protected]>
Fri May 29 17:08:23 2009 Nobuyoshi Nakada <[email protected]>

* hash.c (rb_hash_inspect): rescurses with defined ID to get rid
of extra call. [ruby-core:23601]
* hash.c (Init_Hash): made #to_s an alias to #inspect to reduce
the result of recursive array. a patch from ujihisa a
[ruby-core:23601]. [ruby-dev:38555]

Fri May 29 09:30:00 2009 Nobuyoshi Nakada <[email protected]>

Expand Down
22 changes: 9 additions & 13 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,37 +1231,33 @@ rb_hash_to_a(VALUE hash)
}

static int
inspect_i(VALUE key, VALUE value, VALUE args)
inspect_i(VALUE key, VALUE value, VALUE str)
{
VALUE *arg = (VALUE *)args;
VALUE str = arg[0], str2;
ID funcid = (ID)arg[1];
VALUE str2;

if (key == Qundef) return ST_CONTINUE;
if (RSTRING_LEN(str) > 1) {
rb_str_cat2(str, ", ");
}
str2 = rb_obj_as_string(rb_funcall(key, funcid, 0, 0));
str2 = rb_inspect(key);
rb_str_buf_append(str, str2);
OBJ_INFECT(str, str2);
rb_str_buf_cat2(str, "=>");
str2 = rb_obj_as_string(rb_funcall(value, funcid, 0, 0));
str2 = rb_inspect(value);
rb_str_buf_append(str, str2);
OBJ_INFECT(str, str2);

return ST_CONTINUE;
}

static VALUE
inspect_hash(VALUE hash, VALUE funcname, int recur)
inspect_hash(VALUE hash, VALUE dummy, int recur)
{
VALUE str, args[2];
VALUE str;

if (recur) return rb_usascii_str_new2("{...}");
str = rb_str_buf_new2("{");
args[0] = str;
args[1] = funcname;
rb_hash_foreach(hash, inspect_i, (VALUE)args);
rb_hash_foreach(hash, inspect_i, str);
rb_str_buf_cat2(str, "}");
OBJ_INFECT(str, hash);

Expand All @@ -1284,7 +1280,7 @@ rb_hash_inspect(VALUE hash)
{
if (RHASH_EMPTY_P(hash))
return rb_usascii_str_new2("{}");
return rb_exec_recursive(inspect_hash, hash, (VALUE)rb_frame_this_func());
return rb_exec_recursive(inspect_hash, hash, 0);
}

/*
Expand Down Expand Up @@ -2628,8 +2624,8 @@ Init_Hash(void)

rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
rb_define_method(rb_cHash,"to_a", rb_hash_to_a, 0);
rb_define_method(rb_cHash,"to_s", rb_hash_inspect, 0);
rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
rb_define_alias(rb_cHash, "to_s", "inspect");

rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
rb_define_method(rb_cHash,"[]", rb_hash_aref, 1);
Expand Down

0 comments on commit 2a6fb96

Please sign in to comment.