diff options
author | Radosław Bułat <[email protected]> | 2020-12-18 19:11:35 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-19 03:11:35 +0900 |
commit | 5944c4b3cfbbf8b774a4a76ca71ab9f71c71d1b0 (patch) | |
tree | 20ef7b2904d257f3344fff54517ec2cf717b5960 /object.c | |
parent | f6641d73764115c3f6bdf435a381711d894300b6 (diff) |
attr_reader, attr_writer, attr_accessor and attr methods returns array of symbols (#3935)
Co-authored-by: Yusuke Endoh <[email protected]>
Notes
Notes:
Merged-By: mame <[email protected]>
Diffstat (limited to 'object.c')
-rw-r--r-- | object.c | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -2264,17 +2264,21 @@ id_for_attr(VALUE obj, VALUE name) * value of each instance variable. Equivalent to calling * ``<code>attr</code><i>:name</i>'' 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; i<argc; i++) { - rb_attr(klass, id_for_attr(klass, argv[i]), TRUE, FALSE, TRUE); + ID id = id_for_attr(klass, argv[i]); + rb_attr(klass, id, TRUE, FALSE, TRUE); + rb_ary_push(names, ID2SYM(id)); } - return Qnil; + return names; } /** @@ -2286,6 +2290,7 @@ rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass) * The first form is equivalent to #attr_reader. * The second form is equivalent to <code>attr_accessor(name)</code> but deprecated. * The last form is equivalent to <code>attr_reader(name)</code> 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 * <i>symbol</i><code>.id2name</code>. * 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<argc; i++) { - rb_attr(klass, id_for_attr(klass, argv[i]), FALSE, TRUE, TRUE); + ID id = id_for_attr(klass, argv[i]); + rb_attr(klass, id, FALSE, TRUE, TRUE); + rb_ary_push(names, rb_str_intern(rb_sprintf("%"PRIsVALUE"=", ID2SYM(id)))); } - return Qnil; + return names; } /* @@ -2333,6 +2347,7 @@ rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass) * (<code>@name</code>) and a corresponding access method to read it. * Also creates a method called <code>name=</code> to set the attribute. * String arguments are converted to symbols. + * Returns an array of defined methods names as symbols. * * module Mod * attr_accessor(:one, :two) @@ -2344,11 +2359,17 @@ static VALUE rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass) { int i; + VALUE names = rb_ary_new2(argc * 2); for (i=0; i<argc; i++) { - rb_attr(klass, id_for_attr(klass, argv[i]), TRUE, TRUE, TRUE); + ID id = id_for_attr(klass, argv[i]); + VALUE idv = ID2SYM(id); + + rb_attr(klass, id, TRUE, TRUE, TRUE); + rb_ary_push(names, idv); + rb_ary_push(names, rb_str_intern(rb_sprintf("%"PRIsVALUE"=", idv))); } - return Qnil; + return names; } /* |