Project

General

Profile

Feature #9453 ยป attr_rv.patch

Patch to add return values to attr* methods - jballanc (Joshua Ballanco), 01/26/2014 05:51 PM

View differences:

object.c
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
......
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);
}
......
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;
}
/*
......
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;
}
/*
test/ruby/test_module.rb
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
    (1-1/1)