Project

General

Profile

Actions

Feature #16817

open

attr_predicate or attr_query or attr_something for ? methods

Added by robotdana (Dana Sherson) almost 4 years ago. Updated about 2 years ago.

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

Description

I would like attr_predicate like attr_reader etc.

attr_predicate :cat would define a method named :cat? that returned the value for ivar @cat.

I would like this because i find myself writing this:

def cat?
  @cat
end

which is fine once, but becomes tedious with many attributes

Another possibility is allowing attr_reader :cat? to define a method that returns the value for ivar @cat. currently it raises NameError (invalid attribute name cat?')`

Thank you

Updated by sambostock (Sam Bostock) over 2 years ago

attr_predicate :cat would define a method named :cat? that returned the value for ivar @cat.

I think it would make sense for the predicate method to convert the value to true or false before returning it, to make a clear distinction between a regular "getter" method, and a "predicate" method.

def cat?
  !!@cat
end
Actions #2

Updated by hsbt (Hiroshi SHIBATA) about 2 years ago

  • Project changed from 14 to Ruby master
Actions

Also available in: Atom PDF

Like0
Like0Like0