Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds a return value for attr* methods #517

Open
wants to merge 2 commits into from

2 participants

@jballanc

This pull request changes attr, attr_reader, attr_writer, and
attr_accessor so that the now return an array of symbols representing
the methods that have effectively been added. This matches with the
behavior of def since the 2.1.0 release.

@zzak
Collaborator

@jballanc I think this needs to be rebased

@jballanc

Thanks for the heads-up @zzak ... Rebased against trunk.

@jballanc

The Travis failure is in compiling enc/utf_8.c, which is unrelated to this diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 71 additions and 8 deletions.
  1. +50 −8 object.c
  2. +21 −0 test/ruby/test_module.rb
View
58 object.c
@@ -1946,6 +1946,18 @@ rb_check_attr_id(ID id)
return id;
}
+static void
+mod_attr(int argc, VALUE *argv, VALUE klass, int read, int write, int ex)
+{
+ int i;
+ ID id;
+
+ for (i=0; i<argc; i++) {
+ id = id_for_attr(argv[i]);
+ rb_attr(klass, id, read, write, ex);
+ }
+}
+
/*
* call-seq:
* attr_reader(symbol, ...) -> nil
@@ -1963,20 +1975,35 @@ static VALUE
rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
{
int i;
+ ID id;
+ VALUE ret;
+ mod_attr(argc, argv, klass, TRUE, FALSE, TRUE);
+
+ ret = rb_ary_new_capa(argc);
for (i=0; i<argc; i++) {
- rb_attr(klass, id_for_attr(argv[i]), TRUE, FALSE, TRUE);
+ id = id_for_attr(argv[i]);
+ rb_ary_push(ret, ID2SYM(id));
}
- return Qnil;
+
+ return ret;
}
VALUE
rb_mod_attr(int argc, VALUE *argv, VALUE klass)
{
+ ID id;
+
if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
+ id = id_for_attr(argv[0]);
rb_warning("optional boolean argument is obsoleted");
- rb_attr(klass, id_for_attr(argv[0]), 1, RTEST(argv[1]), TRUE);
- return Qnil;
+ rb_attr(klass, id, 1, RTEST(argv[1]), TRUE);
+
+ if (RTEST(argv[1])) {
+ return rb_ary_new_from_args(2, ID2SYM(id), ID2SYM(rb_id_attrset(id)));
+ } else {
+ return rb_ary_new_from_args(1, ID2SYM(id));
+ }
}
return rb_mod_attr_reader(argc, argv, klass);
}
@@ -1995,11 +2022,18 @@ static VALUE
rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
{
int i;
+ ID id;
+ VALUE ret;
+
+ mod_attr(argc, argv, klass, FALSE, TRUE, TRUE);
+ ret = rb_ary_new_capa(argc);
for (i=0; i<argc; i++) {
- rb_attr(klass, id_for_attr(argv[i]), FALSE, TRUE, TRUE);
+ id = rb_id_attrset(id_for_attr(argv[i]));
+ rb_ary_push(ret, ID2SYM(id));
}
- return Qnil;
+
+ return ret;
}
/*
@@ -2023,11 +2057,19 @@ static VALUE
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
{
int i;
+ ID id;
+ VALUE ret;
+
+ mod_attr(argc, argv, klass, TRUE, TRUE, TRUE);
+ ret = rb_ary_new_capa(2*argc);
for (i=0; i<argc; i++) {
- rb_attr(klass, id_for_attr(argv[i]), TRUE, TRUE, TRUE);
+ id = id_for_attr(argv[i]);
+ rb_ary_push(ret, ID2SYM(id));
+ rb_ary_push(ret, ID2SYM(rb_id_attrset(id)));
}
- return Qnil;
+
+ return ret;
}
/*
View
21 test/ruby/test_module.rb
@@ -821,6 +821,27 @@ def test_attr
end
end
+ def test_attr_return_values
+ a, b, c = nil, nil, nil
+ Class.new do
+ a = attr :foo
+ b = attr :bar, :baz
+ c = attr :qux, true
+ end
+ assert_equal([:foo], a)
+ assert_equal([:bar, :baz], b)
+ assert_equal([:qux, :qux=], c)
+ a, b, c = nil, nil, nil
+ Class.new do
+ a = attr_reader :foo
+ b = attr_writer :bar, :baz
+ c = attr_accessor :qux
+ end
+ assert_equal([:foo], a)
+ assert_equal([:bar=, :baz=], b)
+ assert_equal([:qux, :qux=], c)
+ end
+
def test_undef
c = Class.new
assert_raise(NameError) do
Something went wrong with that request. Please try again.