Actions
Feature #21555
closedAdd support for predicate attribute reader names
Feature #21555:
Add support for predicate attribute reader names
Status:
Rejected
Assignee:
-
Target version:
-
Description
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:
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:
class Example
attr_reader :valid?, :meaning
def initialize
@valid = [true, false].sample
@meaning = 42
end
end
Example.new.valid? #=> true
Actions