[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77809] [Ruby trunk Feature#12046] Allow attr_reader :foo? to define instance method foo? for accessing @foo
From:
svoop@...
Date:
2016-10-29 13:37:05 UTC
List:
ruby-core #77809
Issue #12046 has been updated by Sven Schwyn.
> 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:
class Foobar
attr_accessor :color
attr_writer :transparent
def transparent?
@transparent
end
end
Even though the transparent reader is trivial, it has to be explicitly added and thus is not visible in the list of other trivial accessors such as color.
If tailing question marks are ignored when trivial writers are created, the code would look as follows:
class Foobar
attr_accessor :color, :transparent?
end
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.
----------------------------------------
Feature #12046: Allow attr_reader :foo? to define instance method foo? for accessing @foo
https://bugs.ruby-lang.org/issues/12046#change-61115
* Author: Kenta Murata
* Status: Rejected
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
Now we should write an accessor method of a boolean instance variable like
```ruby
def foo?
@foo
end
```
But I want to write it by using attr_reader like:
```ruby
attr_reader :foo?
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>