Project

General

Profile

Actions

Feature #12046

closed

Allow attr_reader :foo? to define instance method foo? for accessing @foo

Added by mrkn (Kenta Murata) almost 9 years ago. Updated about 8 years ago.

Status:
Rejected
Target version:
-
[ruby-core:73644]

Description

Now we should write an accessor method of a boolean instance variable like

def foo?
  @foo
end

But I want to write it by using attr_reader like:

attr_reader :foo?

Related issues 5 (3 open2 closed)

Related to Ruby master - Feature #11167: Allow an attr_ variant for query-methods that end with a question mark '?' character, such as: def foo? returning @fooRejectedActions
Related to Ruby master - Feature #5781: Query attributes (attribute methods ending in `?` mark)Assignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #15991: Allow questionmarks in variable namesAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #19708: Support `attr_reader :foo?`OpenActions
Is duplicate of Ruby master - Feature #10720: A proposal for something like: attr_reader :foo? - with the trailing '?' question markRejectedActions
Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

  • Is duplicate of Feature #10720: A proposal for something like: attr_reader :foo? - with the trailing '?' question mark added

Updated by shevegen (Robert A. Heiler) almost 9 years ago

Yay! Sometimes things just take a while. :)

Updated by matz (Yukihiro Matsumoto) almost 9 years ago

  • Status changed from Open to Rejected

as a rule, attr_reader x creates an instance variable @x, but we cannot have @x?.
that's the reason, we reject attr_reader :foo?. If you have any concrete use-case for
the new (more complex) behavior, please tell me.

Matz.

Updated by svoop (Sven Schwyn) about 8 years ago

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 part of the list of other trivial accessors such as color.

If tailing question marks are ignored when trivial writers are created, the code looks 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.

Actions #5

Updated by mame (Yusuke Endoh) over 5 years ago

  • Related to 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 mame (Yusuke Endoh) over 5 years ago

  • Related to Feature #5781: Query attributes (attribute methods ending in `?` mark) added
Actions #7

Updated by mrkn (Kenta Murata) about 5 years ago

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

Updated by byroot (Jean Boussier) over 1 year ago

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0