@@ -41,7 +41,8 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
41
41
42
42
static ID i_to_s , i_to_json , i_new , i_indent , i_space , i_space_before ,
43
43
i_object_nl , i_array_nl , i_check_circular , i_max_nesting ,
44
- i_allow_nan , i_pack , i_unpack , i_create_id , i_extend , i_key_p ;
44
+ i_allow_nan , i_pack , i_unpack , i_create_id , i_extend , i_key_p ,
45
+ i_aref , i_send , i_respond_to_p ;
45
46
46
47
typedef struct JSON_Generator_StateStruct {
47
48
VALUE indent ;
@@ -584,6 +585,20 @@ static VALUE cState_to_h(VALUE self)
584
585
return result ;
585
586
}
586
587
588
+ /*
589
+ * call-seq: [](name)
590
+ *
591
+ * Return the value returned by method +name+.
592
+ */
593
+ static VALUE cState_aref (VALUE self , VALUE name )
594
+ {
595
+ GET_STATE (self );
596
+ if (RTEST (rb_funcall (self , i_respond_to_p , 1 , name ))) {
597
+ return rb_funcall (self , i_send , 1 , name );
598
+ } else {
599
+ return Qnil ;
600
+ }
601
+ }
587
602
588
603
/*
589
604
* call-seq: new(opts = {})
@@ -884,6 +899,7 @@ void Init_generator()
884
899
rb_define_method (cState , "forget" , cState_forget , 1 );
885
900
rb_define_method (cState , "configure" , cState_configure , 1 );
886
901
rb_define_method (cState , "to_h" , cState_to_h , 0 );
902
+ rb_define_method (cState , "[]" , cState_aref , 1 );
887
903
888
904
mGeneratorMethods = rb_define_module_under (mGenerator , "GeneratorMethods" );
889
905
mObject = rb_define_module_under (mGeneratorMethods , "Object" );
@@ -926,6 +942,9 @@ void Init_generator()
926
942
i_create_id = rb_intern ("create_id" );
927
943
i_extend = rb_intern ("extend" );
928
944
i_key_p = rb_intern ("key?" );
945
+ i_aref = rb_intern ("[]" );
946
+ i_send = rb_intern ("__send__" );
947
+ i_respond_to_p = rb_intern ("respond_to?" );
929
948
#ifdef HAVE_RUBY_ENCODING_H
930
949
mEncoding_UTF_8 = rb_funcall (rb_path2class ("Encoding" ), rb_intern ("find" ), 1 , rb_str_new2 ("utf-8" ));
931
950
i_encoding = rb_intern ("encoding" );
0 commit comments