From: nobu.nokada@... Date: 2005-09-17T09:16:34+09:00 Subject: Re: proposed attr writer patch Hi, At Sat, 17 Sep 2005 05:01:16 +0900, Daniel Berger wrote in [ruby-core:05796]: > IIRC, this would also fix the issue with "?" and "!" in struct members > (but I may be remembering incorrectly). > > BTW, Evan Webb has already added this patch to Sydney. It doesn't work. $ ./ruby -e 'class X;attr_accessor :foo?; end; x=X.new; x.foo=1; p x.foo?' -e:1:in `attr_accessor': invalid attribute name `foo?' (NameError) from -e:1 And: + sprintf(buf, "%s=", name); buf and name point same area here, but I'm not sure if this behavior is guaranteed. Index: eval.c =================================================================== RCS file: /cvs/ruby/src/ruby/eval.c,v retrieving revision 1.831 diff -U2 -p -r1.831 eval.c --- eval.c 14 Sep 2005 08:30:15 -0000 1.831 +++ eval.c 17 Sep 2005 00:14:25 -0000 @@ -624,5 +624,5 @@ rb_attr(VALUE klass, ID id, int read, in const char *name; char *buf; - ID attriv; + ID attriv, rid = id; int noex; size_t len; @@ -644,17 +644,22 @@ rb_attr(VALUE klass, ID id, int read, in } - if (!rb_is_local_id(id) && !rb_is_const_id(id)) { - rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id)); - } name = rb_id2name(id); if (!name) { rb_raise(rb_eArgError, "argument needs to be symbol or string"); } - len = strlen(name)+2; - buf = ALLOCA_N(char,len); - snprintf(buf, len, "@%s", name); + len = strlen(name); + buf = ALLOCA_N(char,len+2); + snprintf(buf, len+2, "@%s", name); + if (name[len-1] == '?' || name[len-1] == '!') { + buf[len--] = 0; + id = rb_intern(buf+1); + } + if (!rb_is_local_id(id) && !rb_is_const_id(id)) { + rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id)); + } + attriv = rb_intern(buf); if (read) { - rb_add_method(klass, id, NEW_IVAR(attriv), noex); + rb_add_method(klass, rid, NEW_IVAR(attriv), noex); } if (write) { -- Nobu Nakada