Project

General

Profile

Actions

Feature #21555

open

Add support for predicate attribute reader names

Added by shan (Shannon Skipper) 3 days ago. Updated 2 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:123113]

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

Related issues 6 (4 open2 closed)

Related to Ruby - Feature #16817: attr_predicate or attr_query or attr_something for ? methodsOpenActions
Related to Ruby - Feature #15991: Allow questionmarks in variable namesAssignedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby - Feature #5781: Query attributes (attribute methods ending in `?` mark)Assignedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby - Feature #11167: Allow an attr_ variant for query-methods that end with a question mark '?' character, such as: def foo? returning @fooRejectedActions
Is duplicate of Ruby - Feature #12046: Allow attr_reader :foo? to define instance method foo? for accessing @fooRejectedmatz (Yukihiro Matsumoto)Actions
Is duplicate of Ruby - Feature #19708: Support `attr_reader :foo?`OpenActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) 3 days ago

  • Related to Feature #16817: attr_predicate or attr_query or attr_something for ? methods added

Updated by Dan0042 (Daniel DeLorme) 3 days ago

Also related to #5781 #11167 #12046 #15991 #19708
Popular eh?

Updated by byroot (Jean Boussier) 3 days ago

This feature is only supported for attr_reader and attr, not attr_writer or attr_accessor, since setter methods cannot have question marks.

Using the same logic, attr_accessor :enabled? could generate #enabled? and #enabled= without problem. attr_writer :enabled? would be weird though.

Actions #4

Updated by nobu (Nobuyoshi Nakada) 3 days ago

  • Is duplicate of Feature #5781: Query attributes (attribute methods ending in `?` mark) added
Actions #5

Updated by nobu (Nobuyoshi Nakada) 3 days ago

  • Is duplicate of Feature #11167: Allow an attr_ variant for query-methods that end with a question mark '?' character, such as: def foo? returning @foo added
Actions #6

Updated by nobu (Nobuyoshi Nakada) 3 days ago

  • Is duplicate of Feature #12046: Allow attr_reader :foo? to define instance method foo? for accessing @foo added
Actions #7

Updated by nobu (Nobuyoshi Nakada) 3 days ago

  • Related to Feature #15991: Allow questionmarks in variable names added
Actions #8

Updated by nobu (Nobuyoshi Nakada) 3 days ago

Updated by shan (Shannon Skipper) 2 days ago

Dan0042 (Daniel DeLorme) wrote in #note-2:

Also related to #5781 #11167 #12046 #15991 #19708
Popular eh?

Wow! There's already an open ticket for this exact issue even. I apologize for not properly searching before filing. I should have just added a PR link there in retrospect.

byroot (Jean Boussier) wrote in #note-3:

This feature is only supported for attr_reader and attr, not attr_writer or attr_accessor, since setter methods cannot have question marks.

Using the same logic, attr_accessor :enabled? could generate #enabled? and #enabled= without problem. attr_writer :enabled? would be weird though.

I was thinking of attr_writer :enabled? being weird and dismissed attr_accessor :enabled?. I didn't think of the case where you want attr_reader :enabled? and attr_writer :enabled without attr_writer :enabled. I wouldn't mind doing an attr_reader :enabled? with attr_writer :enabled but agree attr_accessor :enabled? makes sense based on the pattern.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0