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:
parent
f6641d7376
commit
5944c4b3cf
Notes:
git
2020-12-19 03:12:00 +09:00
Merged-By: mame <mame@ruby-lang.org>
39
object.c
39
object.c
@ -2264,17 +2264,21 @@ id_for_attr(VALUE obj, VALUE name)
|
|||||||
* value of each instance variable. Equivalent to calling
|
* value of each instance variable. Equivalent to calling
|
||||||
* ``<code>attr</code><i>:name</i>'' on each name in turn.
|
* ``<code>attr</code><i>:name</i>'' on each name in turn.
|
||||||
* String arguments are converted to symbols.
|
* String arguments are converted to symbols.
|
||||||
|
* Returns an array of defined methods names as symbols.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
|
rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
VALUE names = rb_ary_new2(argc);
|
||||||
|
|
||||||
for (i=0; i<argc; i++) {
|
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 first form is equivalent to #attr_reader.
|
||||||
* The second form is equivalent to <code>attr_accessor(name)</code> but deprecated.
|
* 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.
|
* The last form is equivalent to <code>attr_reader(name)</code> but deprecated.
|
||||||
|
* Returns an array of defined methods names as symbols.
|
||||||
*--
|
*--
|
||||||
* \private
|
* \private
|
||||||
* \todo can be static?
|
* \todo can be static?
|
||||||
@ -2295,9 +2300,14 @@ VALUE
|
|||||||
rb_mod_attr(int argc, VALUE *argv, VALUE klass)
|
rb_mod_attr(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
|
if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
|
||||||
rb_category_warning(RB_WARN_CATEGORY_DEPRECATED, "optional boolean argument is obsoleted");
|
ID id = id_for_attr(klass, argv[0]);
|
||||||
rb_attr(klass, id_for_attr(klass, argv[0]), 1, RTEST(argv[1]), TRUE);
|
VALUE names = rb_ary_new();
|
||||||
return Qnil;
|
|
||||||
|
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);
|
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
|
* Creates an accessor method to allow assignment to the attribute
|
||||||
* <i>symbol</i><code>.id2name</code>.
|
* <i>symbol</i><code>.id2name</code>.
|
||||||
* String arguments are converted to symbols.
|
* String arguments are converted to symbols.
|
||||||
|
* Returns an array of defined methods names as symbols.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
|
rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
VALUE names = rb_ary_new2(argc);
|
||||||
|
|
||||||
for (i=0; i<argc; i++) {
|
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.
|
* (<code>@name</code>) and a corresponding access method to read it.
|
||||||
* Also creates a method called <code>name=</code> to set the attribute.
|
* Also creates a method called <code>name=</code> to set the attribute.
|
||||||
* String arguments are converted to symbols.
|
* String arguments are converted to symbols.
|
||||||
|
* Returns an array of defined methods names as symbols.
|
||||||
*
|
*
|
||||||
* module Mod
|
* module Mod
|
||||||
* attr_accessor(:one, :two)
|
* attr_accessor(:one, :two)
|
||||||
@ -2344,11 +2359,17 @@ static VALUE
|
|||||||
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
|
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
VALUE names = rb_ary_new2(argc * 2);
|
||||||
|
|
||||||
for (i=0; i<argc; i++) {
|
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)
|
Module.should have_public_instance_method(:attr_accessor, false)
|
||||||
end
|
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
|
describe "on immediates" do
|
||||||
before :each do
|
before :each do
|
||||||
class Fixnum
|
class Fixnum
|
||||||
|
@ -61,4 +61,10 @@ describe "Module#attr_reader" do
|
|||||||
it "is a public method" do
|
it "is a public method" do
|
||||||
Module.should have_public_instance_method(:attr_reader, false)
|
Module.should have_public_instance_method(:attr_reader, false)
|
||||||
end
|
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
|
end
|
||||||
|
@ -145,4 +145,12 @@ describe "Module#attr" do
|
|||||||
it "is a public method" do
|
it "is a public method" do
|
||||||
Module.should have_public_instance_method(:attr, false)
|
Module.should have_public_instance_method(:attr, false)
|
||||||
end
|
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
|
end
|
||||||
|
@ -61,4 +61,10 @@ describe "Module#attr_writer" do
|
|||||||
it "is a public method" do
|
it "is a public method" do
|
||||||
Module.should have_public_instance_method(:attr_writer, false)
|
Module.should have_public_instance_method(:attr_writer, false)
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user