Issue #5781 has been updated by Thomas Sawyer.


@matz In that case I would propose:

  attr :foo?        
  #=> def foo?; @foo; end

  attr_reader :foo? 
  #=>  def foo?; @foo; end

  attr_writer :foo?
  #=>  def foo=(x); @foo=(x); end

  attr_accessor :foo? 
  #=> attr_reader :foo?; attr_writer :foo?

It's an open question as to whether #attr and/or #attr_reader should define the plan method too. Or if only true/false should be the return value. For the former, I do not think it matters much; maybe #attr just defines the "predicate" method and #attr_reader can define both? As to that later, I think it's better not to do boolean conversion. I believe Ara (or was it Austin?) made good arguments to that effect some years ago, reminding us that conditionals would function the same either way.

----------------------------------------
Feature #5781: Query attributes (attribute methods ending in `?` mark)
https://bugs.ruby-lang.org/issues/5781

Author: Thomas Sawyer
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: 
Target version: 2.0.0


Pretty sure this has come up before, but I'd like to revisit b/c I don't understand why it isn't allowed.

Sometimes I define "query" attributes, and in those cases I'd like the reader method to end in a `?` mark. Currently I have to do:

    # @attribute
    def foo?
      @foo
    end

or, if I don't mind a shadowing bare method,

    attr :foo
    alias_method :foo?, :foo

So why not just allow:

    attr :foo?

Currently this causes an error. But why? It just seems like a waste of potentially cleaner code.


-- 
http://redmine.ruby-lang.org