attr_reader, attr_writer, attr_accessor and attr methods returns array of symbols (#3935)

Co-authored-by: Yusuke Endoh <mame@ruby-lang.org>
This commit is contained in:
Radosław Bułat 2020-12-18 19:11:35 +01:00 коммит произвёл GitHub
Родитель f6641d7376
Коммит 5944c4b3cf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 56 добавлений и 9 удалений

Просмотреть файл

@ -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;
}
/*

Просмотреть файл

@ -67,6 +67,12 @@ describe "Module#attr_accessor" do
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

Просмотреть файл

@ -61,4 +61,10 @@ describe "Module#attr_reader" do
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

Просмотреть файл

@ -145,4 +145,12 @@ describe "Module#attr" do
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

Просмотреть файл

@ -61,4 +61,10 @@ describe "Module#attr_writer" do
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