diff options
| -rw-r--r-- | mrblib/array.rb | 11 | ||||
| -rw-r--r-- | mrblib/hash.rb | 1 | ||||
| -rw-r--r-- | src/array.c | 61 |
3 files changed, 12 insertions, 61 deletions
diff --git a/mrblib/array.rb b/mrblib/array.rb index a7f172879..718553e83 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -85,6 +85,17 @@ class Array end ## + # Private method for Array creation. + # + # ISO 15.2.12.5.31 (x) + def inspect + return "[]" if self.size == 0 + "["+self.map{|x|x.inspect}.join(", ")+"]" + end + # ISO 15.2.12.5.32 (x) + alias to_s inspect + + ## # Delete element with index +key+ def delete(key, &block) while i = self.index(key) diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 74d3d0534..d15fa434e 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -142,6 +142,7 @@ class Hash k.inspect + "=>" + v.inspect }.join(", ")+"}" end + # ISO 15.2.13.4.31 (x) alias to_s inspect # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. diff --git a/src/array.c b/src/array.c index 5dba7c691..42e36feb3 100644 --- a/src/array.c +++ b/src/array.c @@ -984,65 +984,6 @@ mrb_ary_entry(mrb_value ary, mrb_int offset) } static mrb_value -inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) -{ - mrb_int i; - mrb_value s, arystr; - char head[] = { '[' }; - char sep[] = { ',', ' ' }; - char tail[] = { ']' }; - - /* check recursive */ - for (i=0; i<RARRAY_LEN(list); i++) { - if (mrb_obj_equal(mrb, ary, RARRAY_PTR(list)[i])) { - return mrb_str_new_lit(mrb, "[...]"); - } - } - - mrb_ary_push(mrb, list, ary); - - arystr = mrb_str_buf_new(mrb, 64); - mrb_str_buf_cat(mrb, arystr, head, sizeof(head)); - - for (i=0; i<RARRAY_LEN(ary); i++) { - int ai = mrb_gc_arena_save(mrb); - - if (i > 0) { - mrb_str_buf_cat(mrb, arystr, sep, sizeof(sep)); - } - if (mrb_array_p(RARRAY_PTR(ary)[i])) { - s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list); - } - else { - s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); - } - mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); - mrb_gc_arena_restore(mrb, ai); - } - - mrb_str_buf_cat(mrb, arystr, tail, sizeof(tail)); - mrb_ary_pop(mrb, list); - - return arystr; -} - -/* 15.2.12.5.31 (x) */ -/* - * call-seq: - * ary.to_s -> string - * ary.inspect -> string - * - * Creates a string representation of +self+. - */ - -static mrb_value -mrb_ary_inspect(mrb_state *mrb, mrb_value ary) -{ - if (RARRAY_LEN(ary) == 0) return mrb_str_new_lit(mrb, "[]"); - return inspect_ary(mrb, ary, mrb_ary_new(mrb)); -} - -static mrb_value join_ary(mrb_state *mrb, mrb_value ary, mrb_value sep, mrb_value list) { mrb_int i; @@ -1228,8 +1169,6 @@ mrb_init_array(mrb_state *mrb) mrb_define_method(mrb, a, "slice", mrb_ary_aget, MRB_ARGS_ANY()); /* 15.2.12.5.29 */ mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, MRB_ARGS_ANY()); /* 15.2.12.5.30 */ - mrb_define_method(mrb, a, "inspect", mrb_ary_inspect, MRB_ARGS_NONE()); /* 15.2.12.5.31 (x) */ - mrb_define_alias(mrb, a, "to_s", "inspect"); /* 15.2.12.5.32 (x) */ mrb_define_method(mrb, a, "==", mrb_ary_equal, MRB_ARGS_REQ(1)); /* 15.2.12.5.33 (x) */ mrb_define_method(mrb, a, "eql?", mrb_ary_eql, MRB_ARGS_REQ(1)); /* 15.2.12.5.34 (x) */ mrb_define_method(mrb, a, "<=>", mrb_ary_cmp, MRB_ARGS_REQ(1)); /* 15.2.12.5.36 (x) */ |
