From 5944c4b3cfbbf8b774a4a76ca71ab9f71c71d1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Bu=C5=82at?= Date: Fri, 18 Dec 2020 19:11:35 +0100 Subject: [PATCH] attr_reader, attr_writer, attr_accessor and attr methods returns array of symbols (#3935) Co-authored-by: Yusuke Endoh --- object.c | 39 ++++++++++++++++----- spec/ruby/core/module/attr_accessor_spec.rb | 6 ++++ spec/ruby/core/module/attr_reader_spec.rb | 6 ++++ spec/ruby/core/module/attr_spec.rb | 8 +++++ spec/ruby/core/module/attr_writer_spec.rb | 6 ++++ 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/object.c b/object.c index b987e13ae8..49729f644f 100644 --- a/object.c +++ b/object.c @@ -2264,17 +2264,21 @@ id_for_attr(VALUE obj, VALUE name) * value of each instance variable. Equivalent to calling * ``attr:name'' on each name in turn. * String arguments are converted to symbols. + * Returns an array of defined methods names as symbols. */ static VALUE rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass) { int i; + VALUE names = rb_ary_new2(argc); for (i=0; iattr_accessor(name) but deprecated. * The last form is equivalent to attr_reader(name) but deprecated. + * Returns an array of defined methods names as symbols. *-- * \private * \todo can be static? @@ -2295,9 +2300,14 @@ VALUE rb_mod_attr(int argc, VALUE *argv, VALUE klass) { if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) { - rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted"); - rb_attr(klass, id_for_attr(klass, argv[0]), 1, RTEST(argv[1]), TRUE); - return Qnil; + ID id = id_for_attr(klass, argv[0]); + VALUE names = rb_ary_new(); + + rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted"); + rb_attr(klass, id, 1, RTEST(argv[1]), TRUE); + rb_ary_push(names, ID2SYM(id)); + if (argv[1] == Qtrue) rb_ary_push(names, rb_str_intern(rb_sprintf("%"PRIsVALUE"=", ID2SYM(id)))); + return names; } return rb_mod_attr_reader(argc, argv, klass); } @@ -2310,17 +2320,21 @@ rb_mod_attr(int argc, VALUE *argv, VALUE klass) * Creates an accessor method to allow assignment to the attribute * symbol.id2name. * String arguments are converted to symbols. + * Returns an array of defined methods names as symbols. */ static VALUE rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass) { int i; + VALUE names = rb_ary_new2(argc); for (i=0; i