[前][次][番号順一覧][スレッド一覧]

ruby-changes:49062

From: k0kubun <ko1@a...>
Date: Wed, 13 Dec 2017 00:12:03 +0900 (JST)
Subject: [ruby-changes:49062] k0kubun:r61181 (trunk): struct.c: show `keyword_init: true` on inspect

k0kubun	2017-12-13 00:03:45 +0900 (Wed, 13 Dec 2017)

  New Revision: 61181

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61181

  Log:
    struct.c: show `keyword_init: true` on inspect
    
    for debugging if it's specified for the Struct class.
    This follows up r61137.
    
    We don't provide a method to check it because I don't think of any use
    case, but showing this to inspect would be helpful for debugging if
    someone is debugging whether keyword_init is properly enabled or not.
    
    In this commit, I didn't show `keyword_init: false` because of backward
    compatibility. Ideally any application should not depend on the behavior
    of inspect, but I don't have strong motivation to break it too.
    
    [close GH-1773]

  Modified files:
    trunk/struct.c
    trunk/test/ruby/test_struct.rb
Index: struct.c
===================================================================
--- struct.c	(revision 61180)
+++ struct.c	(revision 61181)
@@ -295,6 +295,16 @@ define_aset_method(VALUE nstr, VALUE nam https://github.com/ruby/ruby/blob/trunk/struct.c#L295
 }
 
 static VALUE
+rb_struct_s_inspect(VALUE klass)
+{
+    VALUE inspect = rb_class_name(klass);
+    if (RTEST(struct_ivar_get(klass, id_keyword_init))) {
+	rb_str_cat_cstr(inspect, "(keyword_init: true)");
+    }
+    return inspect;
+}
+
+static VALUE
 setup_struct(VALUE nstr, VALUE members)
 {
     const VALUE *ptr_members;
@@ -306,6 +316,7 @@ setup_struct(VALUE nstr, VALUE members) https://github.com/ruby/ruby/blob/trunk/struct.c#L316
     rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
     rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
     rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
+    rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0);
     ptr_members = RARRAY_CONST_PTR(members);
     len = RARRAY_LEN(members);
     for (i=0; i< len; i++) {
Index: test/ruby/test_struct.rb
===================================================================
--- test/ruby/test_struct.rb	(revision 61180)
+++ test/ruby/test_struct.rb	(revision 61181)
@@ -102,6 +102,8 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L102
     assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(1, b: 2) }
     assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(a: 1, b: 2, c: 3) }
     assert_equal @Struct::KeywordInitTrue.new(a: 1, b: 2).values, @Struct::KeywordInitFalse.new(1, 2).values
+    assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect
+    assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect
 
     @Struct.instance_eval do
       remove_const(:KeywordInitTrue)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]