Project

General

Profile

Actions

Feature #19708

open

Support `attr_reader :foo?`

Added by AMomchilov (Alexander Momchilov) 11 months ago. Updated 11 months ago.

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

Description

Creating reader methods with attr_reader is preferable over defining them by hand, not only because it's more convenient, but also because it hits a fast path in MRI (see VM_METHOD_TYPE_IVAR).

Since local and instance variables can't end with ?, you can't use attr_reader to define predicate-style methods for boolean values, like:

class Person
  attr_reader :name, :age, :active? # invalid attribute name `active?' (NameError)
  
  def initialize
    @name = "Bob"
    @age = 30
    @active = true
  end
end

It would be nice if attr_reader (and friends) could behave like:

def active? = @active

(attr_writer and attr_accessor couldn't do the same, since def active?=(new_value) isn't valid, so they'd probably stick with def active=(new_value))


Related issues 2 (1 open1 closed)

Related to Ruby master - Feature #12046: Allow attr_reader :foo? to define instance method foo? for accessing @fooRejectedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #5781: Query attributes (attribute methods ending in `?` mark)Assignedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like2
Like0Like0Like0Like0Like0Like0Like1Like0Like0