Actions
Feature #12046
closedAllow attr_reader :foo? to define instance method foo? for accessing @foo
Feature #12046:
Allow attr_reader :foo? to define instance method foo? for accessing @foo
Description
Actions
Added by mrkn (Kenta Murata) over 10 years ago. Updated over 9 years ago.
Description
Yay! Sometimes things just take a while. :)
as a rule, attr_reader x creates an instance variable @x, but we cannot have @x?.
that's the reason, we reject attr_reader :foo?. If you have any concrete use-case for
the new (more complex) behavior, please tell me.
Matz.
If you have any concrete use-case for the new (more complex) behavior, please tell me.
This feature request has been rejected more than once, so I'm most likely doing this in vain. But the obvious use-case would be better readability. Right now, the way to do it is:
Even though the transparent reader is trivial, it has to be explicitly added and thus is not part of the list of other trivial accessors such as color.
If tailing question marks are ignored when trivial writers are created, the code looks as follows:
The writer remains "transparent=", but it could reject any non-boolean values. The reader would be "transparent" with an alias "transparent?"
It's just a little magic to remove crust. And it wouldn't break any existing code because accessors and writers with tailing question marks raise "invalid attribute name" as of now.