From: Daniel Berger Date: 2005-09-17T05:01:16+09:00 Subject: proposed attr writer patch Hi all, Currently, if you do something like this: attr_accessor :foo?, :bar! You end up with "bar!=", "foo?=", "bar!", and "foo?". The problem is that "bar!=" and "foo?=" are both pointless and syntactically invalid. Wouldn't it be better if accessors followed the DWIM approach so that we end up with something both useful and syntactically valid? Below is a patch so that attr_accessor handles "?" and "!" better (IMO), so that, using my original example, you end up with "bar=", "bar!", "foo=", "foo?". 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. Regards, Dan PS - The diff is against 1.8.3p2 --- eval.orig Fri Sep 16 13:46:40 2005 +++ eval.c Fri Sep 16 13:47:59 2005 @@ -680,15 +680,25 @@ 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); + +/* Detect ? and ! endings */ + len = strlen(name); + buf = ALLOCA_N(char,len+2); + sprintf(buf, "@%s", name); + + if(name[len-1] == '?' || name[len-1] == '!') { + buf[len] = 0; /* remove the last character */ + name = buf + 1; /* reassign name so if write is set, it uses the + short var name */ + } attriv = rb_intern(buf); if (read) { rb_add_method(klass, id, NEW_IVAR(attriv), noex); } if (write) { - rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex); + sprintf(buf, "%s=", name); + id = rb_intern(buf); + rb_add_method(klass, id, NEW_ATTRSET(attriv), noex); } }