@@ -42,6 +42,50 @@ mrb_obj_basic_to_s_p(mrb_state *mrb, mrb_value obj)
4242 return mrb_func_basic_p (mrb , obj , MRB_SYM (to_s ), mrb_any_to_s );
4343}
4444
45+ struct inspect_i {
46+ mrb_value obj , str ;
47+ };
48+
49+ static int
50+ inspect_i (mrb_state * mrb , mrb_sym sym , mrb_value v , void * p )
51+ {
52+ struct inspect_i * a = (struct inspect_i * )p ;
53+ if (mrb_nil_p (a -> str )) {
54+ const char * cn = mrb_obj_classname (mrb , a -> obj );
55+ a -> str = mrb_str_new_capa (mrb , 30 );
56+
57+ mrb_str_cat_lit (mrb , a -> str , "-<" );
58+ mrb_str_cat_cstr (mrb , a -> str , cn );
59+ mrb_str_cat_lit (mrb , a -> str , ":" );
60+ mrb_str_cat_str (mrb , a -> str , mrb_ptr_to_str (mrb , mrb_obj_ptr (a -> obj )));
61+
62+ if (MRB_RECURSIVE_UNARY_P (mrb , MRB_SYM (inspect ), a -> obj )) {
63+ mrb_str_cat_lit (mrb , a -> str , " ..." );
64+ return 1 ;
65+ }
66+ }
67+
68+ const char * s ;
69+ mrb_int len ;
70+ mrb_value ins ;
71+ char * sp = RSTRING_PTR (a -> str );
72+
73+ /* need not to show internal data */
74+ if (sp [0 ] == '-' ) { /* first element */
75+ sp [0 ] = '#' ;
76+ mrb_str_cat_lit (mrb , a -> str , " " );
77+ }
78+ else {
79+ mrb_str_cat_lit (mrb , a -> str , ", " );
80+ }
81+ s = mrb_sym_name_len (mrb , sym , & len );
82+ mrb_str_cat (mrb , a -> str , s , len );
83+ mrb_str_cat_lit (mrb , a -> str , "=" );
84+ ins = mrb_inspect (mrb , v );
85+ mrb_str_cat_str (mrb , a -> str , ins );
86+ return 0 ;
87+ }
88+
4589/* 15.3.1.3.17 */
4690/*
4791 * call-seq:
@@ -60,7 +104,13 @@ MRB_API mrb_value
60104mrb_obj_inspect (mrb_state * mrb , mrb_value obj )
61105{
62106 if (mrb_object_p (obj ) && mrb_obj_basic_to_s_p (mrb , obj )) {
63- return mrb_obj_iv_inspect (mrb , mrb_obj_ptr (obj ));
107+ struct inspect_i a = { obj , mrb_nil_value () };
108+ mrb_iv_foreach (mrb , obj , inspect_i , & a );
109+ if (!mrb_nil_p (a .str )) {
110+ mrb_assert (mrb_string_p (a .str ));
111+ mrb_str_cat_lit (mrb , a .str , ">" );
112+ return a .str ;
113+ }
64114 }
65115 return mrb_any_to_s (mrb , obj );
66116}
0 commit comments