attr_rv.patch

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

Download (3.23 KB)

View differences:

object.c
1946 1946
    return id;
1947 1947
}
1948 1948

  
1949
static void
1950
mod_attr(int argc, VALUE *argv, VALUE klass, int read, int write, int ex)
1951
{
1952
    int i;
1953
    ID id;
1954

  
1955
    for (i=0; i<argc; i++) {
1956
        id = id_for_attr(argv[i]);
1957
        rb_attr(klass, id, read, write, ex);
1958
    }
1959
}
1960

  
1949 1961
/*
1950 1962
 *  call-seq:
1951 1963
 *     attr_reader(symbol, ...)  -> nil
......
1963 1975
rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
1964 1976
{
1965 1977
    int i;
1978
    ID id;
1979
    VALUE ret;
1966 1980

  
1981
    mod_attr(argc, argv, klass, TRUE, FALSE, TRUE);
1982

  
1983
    ret = rb_ary_new_capa(argc);
1967 1984
    for (i=0; i<argc; i++) {
1968
	rb_attr(klass, id_for_attr(argv[i]), TRUE, FALSE, TRUE);
1985
        id = id_for_attr(argv[i]);
1986
        rb_ary_push(ret, ID2SYM(id));
1969 1987
    }
1970
    return Qnil;
1988

  
1989
    return ret;
1971 1990
}
1972 1991

  
1973 1992
VALUE
1974 1993
rb_mod_attr(int argc, VALUE *argv, VALUE klass)
1975 1994
{
1995
    ID id;
1996

  
1976 1997
    if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
1998
        id = id_for_attr(argv[0]);
1977 1999
	rb_warning("optional boolean argument is obsoleted");
1978
	rb_attr(klass, id_for_attr(argv[0]), 1, RTEST(argv[1]), TRUE);
1979
	return Qnil;
2000
	rb_attr(klass, id, 1, RTEST(argv[1]), TRUE);
2001

  
2002
        if (RTEST(argv[1])) {
2003
            return rb_ary_new_from_args(2, ID2SYM(id), ID2SYM(rb_id_attrset(id)));
2004
        } else {
2005
            return rb_ary_new_from_args(1, ID2SYM(id));
2006
        }
1980 2007
    }
1981 2008
    return rb_mod_attr_reader(argc, argv, klass);
1982 2009
}
......
1995 2022
rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
1996 2023
{
1997 2024
    int i;
2025
    ID id;
2026
    VALUE ret;
2027

  
2028
    mod_attr(argc, argv, klass, FALSE, TRUE, TRUE);
1998 2029

  
2030
    ret = rb_ary_new_capa(argc);
1999 2031
    for (i=0; i<argc; i++) {
2000
	rb_attr(klass, id_for_attr(argv[i]), FALSE, TRUE, TRUE);
2032
        id = rb_id_attrset(id_for_attr(argv[i]));
2033
        rb_ary_push(ret, ID2SYM(id));
2001 2034
    }
2002
    return Qnil;
2035

  
2036
    return ret;
2003 2037
}
2004 2038

  
2005 2039
/*
......
2023 2057
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
2024 2058
{
2025 2059
    int i;
2060
    ID id;
2061
    VALUE ret;
2062

  
2063
    mod_attr(argc, argv, klass, TRUE, TRUE, TRUE);
2026 2064

  
2065
    ret = rb_ary_new_capa(2*argc);
2027 2066
    for (i=0; i<argc; i++) {
2028
	rb_attr(klass, id_for_attr(argv[i]), TRUE, TRUE, TRUE);
2067
        id = id_for_attr(argv[i]);
2068
        rb_ary_push(ret, ID2SYM(id));
2069
        rb_ary_push(ret, ID2SYM(rb_id_attrset(id)));
2029 2070
    }
2030
    return Qnil;
2071

  
2072
    return ret;
2031 2073
}
2032 2074

  
2033 2075
/*
test/ruby/test_module.rb
816 816
    end
817 817
  end
818 818

  
819
  def test_attr_return_values
820
    a, b, c = nil, nil, nil
821
    Class.new do
822
      a = attr :foo
823
      b = attr :bar, :baz
824
      c = attr :qux, true
825
    end
826
    assert_equal([:foo], a)
827
    assert_equal([:bar, :baz], b)
828
    assert_equal([:qux, :qux=], c)
829
    a, b, c = nil, nil, nil
830
    Class.new do
831
      a = attr_reader :foo
832
      b = attr_writer :bar, :baz
833
      c = attr_accessor :qux
834
    end
835
    assert_equal([:foo], a)
836
    assert_equal([:bar=, :baz=], b)
837
    assert_equal([:qux, :qux=], c)
838
  end
839

  
819 840
  def test_undef
820 841
    c = Class.new
821 842
    assert_raise(NameError) do