ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark)
@ 2011-12-19 19:57 Thomas Sawyer
  2011-12-20 12:52 ` [ruby-core:41754] [ruby-trunk - Feature #5781][Assigned] " Yui NARUSE
                   ` (11 more replies)
  0 siblings, 12 replies; 15+ messages in thread
From: Thomas Sawyer @ 2011-12-19 19:57 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been reported by Thomas Sawyer.

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

Author: Thomas Sawyer
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 1.9.4


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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41754] [ruby-trunk - Feature #5781][Assigned] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
@ 2011-12-20 12:52 ` Yui NARUSE
  2011-12-20 14:03 ` [ruby-core:41755] [ruby-trunk - Feature #5781] " Yukihiro Matsumoto
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: Yui NARUSE @ 2011-12-20 12:52 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by Yui NARUSE.

Status changed from Open to Assigned
Assignee set to Yukihiro Matsumoto
Target version changed from 1.9.4 to 2.0.0


----------------------------------------
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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41755] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
  2011-12-20 12:52 ` [ruby-core:41754] [ruby-trunk - Feature #5781][Assigned] " Yui NARUSE
@ 2011-12-20 14:03 ` Yukihiro Matsumoto
  2011-12-20 14:47   ` [ruby-core:41756] " Jeremy Bopp
  2011-12-20 18:51 ` [ruby-core:41759] " Thomas Sawyer
                   ` (9 subsequent siblings)
  11 siblings, 1 reply; 15+ messages in thread
From: Yukihiro Matsumoto @ 2011-12-20 14:03 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by Yukihiro Matsumoto.


It's mostly because semantics.  attr :a creates a method corresponding to an instance variable @a.  So naively, attr :a? tries to create an instance variables @a? which is not a valid name for a instance variable.

I don't want to allow instance variable names ending '?', just because ? is for predicates, not for variables.
The other option is removing '?' from instance variables.  But as far as I remember no one seriously proposed the idea before, and we haven't got consensus.

Matz.
----------------------------------------
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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41756] Re: [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-20 14:03 ` [ruby-core:41755] [ruby-trunk - Feature #5781] " Yukihiro Matsumoto
@ 2011-12-20 14:47   ` Jeremy Bopp
  2011-12-20 16:06     ` [ruby-core:41757] " Anurag Priyam
  0 siblings, 1 reply; 15+ messages in thread
From: Jeremy Bopp @ 2011-12-20 14:47 UTC (permalink / raw)
  To: ruby-core

On 12/20/2011 08:03 AM, Yukihiro Matsumoto wrote:
> 
> Issue #5781 has been updated by Yukihiro Matsumoto.
> 
> 
> It's mostly because semantics.  attr :a creates a method corresponding to an instance variable @a.  So naively, attr :a? tries to create an instance variables @a? which is not a valid name for a instance variable.
> 
> I don't want to allow instance variable names ending '?', just because ? is for predicates, not for variables.
> The other option is removing '?' from instance variables.  But as far as I remember no one seriously proposed the idea before, and we haven't got consensus.

How about adding an attr_query method that otherwise works identically
to attr_reader but creates a method with a '?' on the end of the given
name instead?

For example, you could use it like this:

class Foo
  attr_query :bar
  attr_writer :bar
end

foo = Foo.new
foo.bar = true
foo.bar?         # => true
foo.bar = false
foo.bar?         # => false

-Jeremy

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41757] Re: [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-20 14:47   ` [ruby-core:41756] " Jeremy Bopp
@ 2011-12-20 16:06     ` Anurag Priyam
  0 siblings, 0 replies; 15+ messages in thread
From: Anurag Priyam @ 2011-12-20 16:06 UTC (permalink / raw)
  To: ruby-core

On Tue, Dec 20, 2011 at 8:17 PM, Jeremy Bopp <jeremy@bopp.net> wrote:
> How about adding an attr_query method that otherwise works identically
> to attr_reader but creates a method with a '?' on the end of the given
> name instead?

I would suggest the name `predicate` instead.

class Foo
  attr :bar
  predicate :bar
end

Alternatively,

On Tue, Dec 20, 2011 at 7:33 PM, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> The other option is removing '?' from instance variables.  But as far as I remember no one seriously proposed the idea before, and we haven't got consensus.

Use `attr`, but if the variable name passed to `attr` contains a '?'
define a predicate corresponding to it too.

class Foo
  attr :bar?
  attr :baz
end

f = Foo.new
f.bar?  #=> nil
f.bar = 'meh'
f.bar    #=> 'meh'
f.bar?  #=> 'meh'

f.baz?  #=> NoMethodError

The predicate could be made to strictly return `true` or `false`
instead of simply behaving like an attribute reader.


The former is perhaps easier to implement, but I would like to use the
latter approach in my code.

-- 
Anurag Priyam

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41759] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
  2011-12-20 12:52 ` [ruby-core:41754] [ruby-trunk - Feature #5781][Assigned] " Yui NARUSE
  2011-12-20 14:03 ` [ruby-core:41755] [ruby-trunk - Feature #5781] " Yukihiro Matsumoto
@ 2011-12-20 18:51 ` Thomas Sawyer
  2011-12-20 18:59 ` [ruby-core:41760] " Thomas Sawyer
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: Thomas Sawyer @ 2011-12-20 18:51 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by Thomas Sawyer.


@jeremy Been down that road, and it's not as clean as you might expect. You end up needing two methods e.g. `attr_query_reader` and `attr_query_accessor` (`attr_query_writer` would be essentially meaningless). Moreover, adding additional attr methods tends to be one of those "Cambrian explosion" deals --there are vast variations people have devised. Check out Rails for examples. So I don't think it's a good precedence for core Ruby. In fact I've always thought it a bit unfortunate that #attr alone wasn't all we needed. 

----------------------------------------
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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41760] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (2 preceding siblings ...)
  2011-12-20 18:51 ` [ruby-core:41759] " Thomas Sawyer
@ 2011-12-20 18:59 ` Thomas Sawyer
  2011-12-20 22:44 ` [ruby-core:41761] " Benoit Daloze
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: Thomas Sawyer @ 2011-12-20 18:59 UTC (permalink / raw)
  To: ruby-core


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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41761] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (3 preceding siblings ...)
  2011-12-20 18:59 ` [ruby-core:41760] " Thomas Sawyer
@ 2011-12-20 22:44 ` Benoit Daloze
  2011-12-21 19:27 ` [ruby-core:41766] " Joshua Ballanco
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: Benoit Daloze @ 2011-12-20 22:44 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by Benoit Daloze.


> So why not just allow: attr :foo?

I agree. I know many people wish for that too.

> matz: The other option is removing '?' from instance variables. But as far as I remember no one seriously proposed the idea before, and we haven't got consensus.

What do you mean by removing '?' from instance variables ?
As you said, '?' is already forbidden in instance variable names.

> Thomas Sawyer: It's an open question as to whether #attr and/or #attr_reader should define the plan method too.

I think the plain (bare) method should not be defined (to keep it as clean and simple as possible), and there should not be any conversion (which could lose information).

Also, since attr* :foo? does not conflict with current uses, I think it's fine to use the usual attr* methods.
----------------------------------------
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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:41766] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (4 preceding siblings ...)
  2011-12-20 22:44 ` [ruby-core:41761] " Benoit Daloze
@ 2011-12-21 19:27 ` Joshua Ballanco
  2012-10-26 21:31 ` [ruby-core:48362] " ko1 (Koichi Sasada)
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: Joshua Ballanco @ 2011-12-21 19:27 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by Joshua Ballanco.


Perhaps one option to consider is to allow extra parameters specifying alternate names for the getters and setters (Obj-C does this for synthesized properties). Something like:

attr_accessor :foo, { :var => :bar, :getter => :is_barable? }, :baz
----------------------------------------
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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:48362] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (5 preceding siblings ...)
  2011-12-21 19:27 ` [ruby-core:41766] " Joshua Ballanco
@ 2012-10-26 21:31 ` ko1 (Koichi Sasada)
  2012-12-23 18:43 ` [ruby-core:51098] " trans (Thomas Sawyer)
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: ko1 (Koichi Sasada) @ 2012-10-26 21:31 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by ko1 (Koichi Sasada).

Target version changed from 2.0.0 to next minor

I changed target to next minor because no discussion on it.

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

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


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://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:51098] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (6 preceding siblings ...)
  2012-10-26 21:31 ` [ruby-core:48362] " ko1 (Koichi Sasada)
@ 2012-12-23 18:43 ` trans (Thomas Sawyer)
  2012-12-24  2:10 ` [ruby-core:51108] " trans (Thomas Sawyer)
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: trans (Thomas Sawyer) @ 2012-12-23 18:43 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by trans (Thomas Sawyer).


=begin
I revisited this b/c in one of my projects it is much needed. To compensate, I created a special extension called (({attr_switch})),

    def attr_switch(name)
      attr_writer name
      module_eval %{
        def #{name}?
          @#{name}
        end
      }
    end

But it has the problem that the (({#source_location})) for the method created is in attr_switch's definition and not where attr_switch is called. And in my case that is a problem. Does anyone know if there is a way to tell it the (({source_location})) should be at caller[0]? I tried adding that info to (({#module_eval})) call, i.e. 

  file, line = *caller[0].split(':')[0..1]
  module_eval %{...}, file, line.to_i

But it didn't work.

So bringing this back to this feature request. I, for one, still would very much like this feature. Sometimes it's just much more convenient. And I'd much rather it just worked out-of-box then me having to fuss with creating a custom attr method (and as I point out above, I can't even get it to work exactly the same).

I took a look at the relevant C code, (({rb_attr()})) in (({vm_method.c})), but I simply do not understand that code enough to adjust it myself. If I did, I would have submitted a patch for this already.

Given what I understand about the new ((*process*)) for changing Ruby, I guess I need a sponsor from core team or a core member of another implementation. Is that correct? If so, is anyone willing to back this?
=end

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

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


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://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:51108] [ruby-trunk - Feature #5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (7 preceding siblings ...)
  2012-12-23 18:43 ` [ruby-core:51098] " trans (Thomas Sawyer)
@ 2012-12-24  2:10 ` trans (Thomas Sawyer)
  2019-07-13 10:31 ` [ruby-core:93739] [Ruby master Feature#5781] " eregontp
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 15+ messages in thread
From: trans (Thomas Sawyer) @ 2012-12-24  2:10 UTC (permalink / raw)
  To: ruby-core


Issue #5781 has been updated by trans (Thomas Sawyer).


=begin
FYI,

  file, line = *caller[0].split(':')[0..1]
  module_eval %{...}, file, line.to_i

Actually this does work. My problem with #source_location stemmed from getting it from the (({attr_writer})) defined method. What I had to do was:

  def attr_switch(name)
    file, line = *caller[0].split(':')[0..1]
    module_eval %{
      def #{name}=(x)
        @#{name}=x
      end
      def #{name}?
        @#{name}
      end
    }, file, line.to_i
  end

In any case, still would be better to have `attr_accessor :x?` work.
=end

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

Author: trans (Thomas Sawyer)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


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://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:93739] [Ruby master Feature#5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (8 preceding siblings ...)
  2012-12-24  2:10 ` [ruby-core:51108] " trans (Thomas Sawyer)
@ 2019-07-13 10:31 ` eregontp
  2019-07-15  9:42 ` [ruby-core:93785] " sudo.nice
  2020-01-10  6:34 ` [ruby-core:96755] " abaelter
  11 siblings, 0 replies; 15+ messages in thread
From: eregontp @ 2019-07-13 10:31 UTC (permalink / raw)
  To: ruby-core

Issue #5781 has been updated by Eregon (Benoit Daloze).


FWIW, a long time ago I tried to implement https://bugs.ruby-lang.org/issues/5781#note-7 in MRI but it turned out to be not so easy and I gave up.

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

* Author: trans (Thomas Sawyer)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:93785] [Ruby master Feature#5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (9 preceding siblings ...)
  2019-07-13 10:31 ` [ruby-core:93739] [Ruby master Feature#5781] " eregontp
@ 2019-07-15  9:42 ` sudo.nice
  2020-01-10  6:34 ` [ruby-core:96755] " abaelter
  11 siblings, 0 replies; 15+ messages in thread
From: sudo.nice @ 2019-07-15  9:42 UTC (permalink / raw)
  To: ruby-core

Issue #5781 has been updated by sudo (Sudo Nice).


How about implementing it similarly to Crystal?

    attr_accessor? :foo

will expand to:

    def foo=(value)
      @value = value
    end

    def foo?
      @value
    end


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

* Author: trans (Thomas Sawyer)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:96755] [Ruby master Feature#5781] Query attributes (attribute methods ending in `?` mark)
  2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
                   ` (10 preceding siblings ...)
  2019-07-15  9:42 ` [ruby-core:93785] " sudo.nice
@ 2020-01-10  6:34 ` abaelter
  11 siblings, 0 replies; 15+ messages in thread
From: abaelter @ 2020-01-10  6:34 UTC (permalink / raw)
  To: ruby-core

Issue #5781 has been updated by anders (Anders Bälter).


sudo (Sudo Nice) wrote:
> How about implementing it similarly to Crystal?
> 
>     attr_accessor? :foo

+1

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

* Author: trans (Thomas Sawyer)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2020-01-10  6:34 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-19 19:57 [ruby-core:41728] [ruby-trunk - Feature #5781][Open] Query attributes (attribute methods ending in `?` mark) Thomas Sawyer
2011-12-20 12:52 ` [ruby-core:41754] [ruby-trunk - Feature #5781][Assigned] " Yui NARUSE
2011-12-20 14:03 ` [ruby-core:41755] [ruby-trunk - Feature #5781] " Yukihiro Matsumoto
2011-12-20 14:47   ` [ruby-core:41756] " Jeremy Bopp
2011-12-20 16:06     ` [ruby-core:41757] " Anurag Priyam
2011-12-20 18:51 ` [ruby-core:41759] " Thomas Sawyer
2011-12-20 18:59 ` [ruby-core:41760] " Thomas Sawyer
2011-12-20 22:44 ` [ruby-core:41761] " Benoit Daloze
2011-12-21 19:27 ` [ruby-core:41766] " Joshua Ballanco
2012-10-26 21:31 ` [ruby-core:48362] " ko1 (Koichi Sasada)
2012-12-23 18:43 ` [ruby-core:51098] " trans (Thomas Sawyer)
2012-12-24  2:10 ` [ruby-core:51108] " trans (Thomas Sawyer)
2019-07-13 10:31 ` [ruby-core:93739] [Ruby master Feature#5781] " eregontp
2019-07-15  9:42 ` [ruby-core:93785] " sudo.nice
2020-01-10  6:34 ` [ruby-core:96755] " abaelter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).