From: Trans Date: 2007-12-14T20:53:40+09:00 Subject: Re: attr_accessor for question methods (?) On Dec 14, 4:18 am, Charles Oliver Nutter wrote: > Trans wrote: > > Like to make a small suggestion for 1.9. It would be great if we could > > do: > > > attr_accessor :foo? > > > With the effect of: > > > def foo? > > @foo > > end > > > def foo=(x) > > @foo=x > > end > > > This is much nice then having to define one attribute plus a > > supplemental "?" method. attr_reader :foo? would work too, of course. > > While it would be rather meaningless for attr_writer, it could work in > > any case --really no need to raise an error. > > > A minor detail, but one that would be convenient to have. > > Perhaps an additional method would be the way to do it? > > question_accessor > predicate_accessor > pred_accessor > boolean_accessor > bool_accessor The more methods you have the more methods you have to support --that has a lot of ramifications for Ruby's developers as well as meta- coders. Currently, attr_accessor and friends throw an error if a symbol ends in a '?'. attr_accessor :foo? NameError: invalid attribute name `foo? So it's simply wasted semantic space, that we can put to use quite easily and in a very sensible way. Adding yet another attribute method (YAAM?) is overkill. I think the attr methods have a tendency to make us forget that all they do is create other methods for us. Since method names can end in "?", it simply stands to reason that the attr methods should allow them too. Also note that I have added such method to Facets, but they are rarely used. I would say ~80% of the time you just want a ? mark on the reader, nothing more, and it is generally just easier to define the method yourself then depend on YAAM. Of course Ara took an interesting approach. His #attribute method *always* defines a reader, writer, and "query" method. T.