diff options
author | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-12-12 15:03:45 +0000 |
---|---|---|
committer | k0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-12-12 15:03:45 +0000 |
commit | 8411f58ed70c1e6dc4e35589c94fd231c4e384d2 (patch) | |
tree | 88b3a8e1b77e3bbf916d1758c8656b587fb3a013 | |
parent | 1dcdb868765c88af741772223702cb98d97c6b2c (diff) |
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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61181 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | struct.c | 11 | ||||
-rw-r--r-- | test/ruby/test_struct.rb | 2 |
2 files changed, 13 insertions, 0 deletions
@@ -295,6 +295,16 @@ define_aset_method(VALUE nstr, VALUE name, VALUE off) } 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) 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++) { diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 817ad8c61e..384c95f85b 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -102,6 +102,8 @@ module TestStruct 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) |