Feature #5781
Query attributes (attribute methods ending in `?` mark)
| Status: | Assigned | Start date: | 12/20/2011 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | - | |||
| Target version: | 2.0.0 |
Description
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.
History
Updated by naruse (Yui NARUSE) 5 months ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
- Target version changed from 1.9.4 to 2.0.0
Updated by matz (Yukihiro Matsumoto) 5 months ago
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.
Updated by javanthropus (Jeremy Bopp) 5 months ago
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
Updated by yeban (Anurag Priyam) 5 months ago
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
Updated by trans (Thomas Sawyer) 5 months ago
@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.
Updated by trans (Thomas Sawyer) 5 months ago
@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.
Updated by Eregon (Benoit Daloze) 5 months ago
> 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.
Updated by shevegen (markus heiler) 5 months ago
attr_query :foo
Would be nice to add a query method ending in foo? to return the instance variable @foo.
I agree that changing current behaviour of attr or attr_reader or attr_writer would be bad.
But a new method "attr_query :foo" would be nice to have. (I don't agree with "predicate :foo",
but I could agree to attr_predicate :foo - I think it would be nice and consistent to include
the attr_ part in such a method name.)
I also agree with matz on instance variables.
@foo? = "hi"
Does not read elegantly and thus should not be possible and not be changed.
But I also agree that there should be a very easy way to generate a
query method ending in "?".
It would make some code shorter.
Updated by jballanc (Joshua Ballanco) 5 months ago
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