[ruby-core:123215] [Ruby Feature#21555] Add support for predicate attribute reader names
From:
"matz (Yukihiro Matsumoto) via ruby-core" <ruby-core@...>
Date:
2025-09-11 05:21:34 UTC
List:
ruby-core #123215
Issue #21555 has been updated by matz (Yukihiro Matsumoto).
Status changed from Open to Rejected
I still ask you to define `def valid? = @valid` manually. It is not much worse than `attr_reader :valid?`.
Matz.
----------------------------------------
Feature #21555: Add support for predicate attribute reader names
https://bugs.ruby-lang.org/issues/21555#change-114545
* Author: shan (Shannon Skipper)
* Status: Rejected
----------------------------------------
After manually aliasing predicate methods many times, I wanted to propose letting `attr_reader` take predicate method names that correspond to instance variables of the base name without a trailing question mark. https://github.com/ruby/ruby/pull/14391
If the base name method doesn't already exist, a predicate name defines a base name method attribute reader to alias then undefines the base name.
For example, this creates an `enabled?` method that reads `@enabled`:
```ruby
attr_reader :enabled?
```
This feature is only supported for `attr_reader` and `attr`, not `attr_writer` or `attr_accessor`, since setter methods cannot have question marks.
Example:
```ruby
class Example
attr_reader :valid?, :meaning
def initialize
@valid = [true, false].sample
@meaning = 42
end
end
Example.new.valid? #=> true
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/