ruby-changes:64302
From: Rados=C5=82aw <ko1@a...>
Date: Sat, 19 Dec 2020 03:11:55 +0900 (JST)
Subject: [ruby-changes:64302] 5944c4b3cf (master): attr_reader, attr_writer, attr_accessor and attr methods returns array of symbols (#3935)
https://git.ruby-lang.org/ruby.git/commit/?id=5944c4b3cf From 5944c4b3cfbbf8b774a4a76ca71ab9f71c71d1b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Bu=C5=82at?= <radek.bulat@g...> Date: Fri, 18 Dec 2020 19:11:35 +0100 Subject: attr_reader, attr_writer, attr_accessor and attr methods returns array of symbols (#3935) Co-authored-by: Yusuke Endoh <mame@r...> diff --git a/object.c b/object.c index b987e13..49729f6 100644 --- a/object.c +++ b/object.c @@ -2264,17 +2264,21 @@ id_for_attr(VALUE obj, VALUE name) https://github.com/ruby/ruby/blob/trunk/object.c#L2264 * 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) https://github.com/ruby/ruby/blob/trunk/object.c#L2290 * 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 https://github.com/ruby/ruby/blob/trunk/object.c#L2300 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) https://github.com/ruby/ruby/blob/trunk/object.c#L2320 * 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) https://github.com/ruby/ruby/blob/trunk/object.c#L2347 * (<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 https://github.com/ruby/ruby/blob/trunk/object.c#L2359 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; } /* diff --git a/spec/ruby/core/module/attr_accessor_spec.rb b/spec/ruby/core/module/attr_accessor_spec.rb index 6a74934..c4133d8 100644 --- a/spec/ruby/core/module/attr_accessor_spec.rb +++ b/spec/ruby/core/module/attr_accessor_spec.rb @@ -67,6 +67,12 @@ describe "Module#attr_accessor" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/attr_accessor_spec.rb#L67 Module.should have_public_instance_method(:attr_accessor, false) end + it "returns an array of defined methods names as symbols" do + Class.new do + (attr_accessor :foo, 'bar').should == [:foo, :foo=, :bar, :bar=] + end + end + describe "on immediates" do before :each do class Fixnum diff --git a/spec/ruby/core/module/attr_reader_spec.rb b/spec/ruby/core/module/attr_reader_spec.rb index 238e3db..0693909 100644 --- a/spec/ruby/core/module/attr_reader_spec.rb +++ b/spec/ruby/core/module/attr_reader_spec.rb @@ -61,4 +61,10 @@ describe "Module#attr_reader" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/attr_reader_spec.rb#L61 it "is a public method" do Module.should have_public_instance_method(:attr_reader, false) end + + it "returns an array of defined methods names as symbols" do + Class.new do + (attr_reader :foo, 'bar').should == [:foo, :bar] + end + end end diff --git a/spec/ruby/core/module/attr_spec.rb b/spec/ruby/core/module/attr_spec.rb index 8b91e77..8c7d27e 100644 --- a/spec/ruby/core/module/attr_spec.rb +++ b/spec/ruby/core/module/attr_spec.rb @@ -145,4 +145,12 @@ describe "Module#attr" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/attr_spec.rb#L145 it "is a public method" do Module.should have_public_instance_method(:attr, false) end + + it "returns an array of defined methods names as symbols" do + Class.new do + (attr :foo, 'bar').should == [:foo, :bar] + (attr :baz, false).should == [:baz] + (attr :qux, true).should == [:qux, :qux=] + end + end end diff --git a/spec/ruby/core/module/attr_writer_spec.rb b/spec/ruby/core/module/attr_writer_spec.rb index e4b193a9..c5a00b8 100644 --- a/spec/ruby/core/module/attr_writer_spec.rb +++ b/spec/ruby/core/module/attr_writer_spec.rb @@ -61,4 +61,10 @@ describe "Module#attr_writer" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/attr_writer_spec.rb#L61 it "is a public method" do Module.should have_public_instance_method(:attr_writer, false) end + + it "returns an array of defined methods names as symbols" do + Class.new do + (attr_writer :foo, 'bar').should == [:foo=, :bar=] + end + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/