@@ -634,6 +634,49 @@ mrb_struct_values_at(mrb_state *mrb, mrb_value self)
634634return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
635635}
636636637+/*
638+ * call-seq:
639+ * struct.to_s -> string
640+ * struct.inspect -> string
641+ *
642+ * Returns a string representation of Data
643+ */
644+static mrb_value
645+mrb_struct_to_s(mrb_state *mrb, mrb_value self)
646+{
647+mrb_value members, ret, cname;
648+mrb_value *mems;
649+mrb_int mlen;
650+651+mrb->c->ci->mid = MRB_SYM(inspect);
652+ret = mrb_str_new_lit(mrb, "#<struct ");
653+int ai = mrb_gc_arena_save(mrb);
654+cname = mrb_class_path(mrb, mrb_class_real(mrb_class(mrb, self)));
655+if (!mrb_nil_p(cname)) {
656+mrb_str_cat_str(mrb, ret, cname);
657+mrb_str_cat_lit(mrb, ret, " ");
658+ }
659+if (mrb_inspect_recursive_p(mrb, self)) {
660+mrb_str_cat_lit(mrb, ret, "...>");
661+return ret;
662+ }
663+members = struct_members(mrb, self);
664+mlen = RARRAY_LEN(members);
665+mems = RARRAY_PTR(members);
666+for (mrb_int i=0; i<mlen; i++) {
667+mrb_int len;
668+const char *name = mrb_sym_name_len(mrb, mrb_symbol(mems[i]), &len);
669+if (i>0) mrb_str_cat_lit(mrb, ret, ", ");
670+mrb_str_cat(mrb, ret, name, len);
671+mrb_str_cat_lit(mrb, ret, "=");
672+mrb_str_cat_str(mrb, ret, mrb_inspect(mrb, mrb_ary_ref(mrb, self, i)));
673+mrb_gc_arena_restore(mrb, ai);
674+ }
675+mrb_str_cat_lit(mrb, ret, ">");
676+677+return ret;
678+}
679+637680/*
638681 * A <code>Struct</code> is a convenient way to bundle a number of
639682 * attributes together, using accessor methods, without having to write
@@ -665,6 +708,8 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
665708mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */
666709mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */
667710mrb_define_method(mrb, st, "eql?", mrb_struct_eql, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */
711+mrb_define_method(mrb, st, "to_s", mrb_struct_to_s, MRB_ARGS_NONE()); /* 15.2.18.4.11(x) */
712+mrb_define_method(mrb, st, "inspect", mrb_struct_to_s, MRB_ARGS_NONE()); /* 15.2.18.4.10(x) */
668713669714mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE());
670715mrb_define_method(mrb, st, "length", mrb_struct_len, MRB_ARGS_NONE());