https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?15064139052011-12-20T21:52:31ZRuby Issue Tracking SystemRuby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228892011-12-20T21:52:31Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> changed from <i>1.9.4</i> to <i>2.0.0</i></li></ul> Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228902011-12-20T23:03:41Zmatz (Yukihiro Matsumoto)matz@ruby-lang.org
<ul></ul><p>It&#39;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.</p>
<p>I don&#39;t want to allow instance variable names ending &#39;?&#39;, just because ? is for predicates, not for variables.<br>
The other option is removing &#39;?&#39; from instance variables. But as far as I remember no one seriously proposed the idea before, and we haven&#39;t got consensus.</p>
<p>Matz.</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228912011-12-20T23:53:09Zjavanthropus (Jeremy Bopp)jeremy@bopp.net
<ul></ul><p>On 12/20/2011 08:03 AM, Yukihiro Matsumoto wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Query attributes (attribute methods ending in `?` mark) (Assigned)" href="https://bugs.ruby-lang.org/issues/5781">#5781</a> has been updated by Yukihiro Matsumoto.</p>
<p>It&#39;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.</p>
<p>I don&#39;t want to allow instance variable names ending &#39;?&#39;, just because ? is for predicates, not for variables.<br>
The other option is removing &#39;?&#39; from instance variables. But as far as I remember no one seriously proposed the idea before, and we haven&#39;t got consensus.</p>
</blockquote>
<p>How about adding an attr_query method that otherwise works identically<br>
to attr_reader but creates a method with a &#39;?&#39; on the end of the given<br>
name instead?</p>
<p>For example, you could use it like this:</p>
<p>class Foo<br>
attr_query :bar<br>
attr_writer :bar<br>
end</p>
<p>foo = Foo.new<br>
foo.bar = true<br>
foo.bar? # =&gt; true<br>
foo.bar = false<br>
foo.bar? # =&gt; false</p>
<p>-Jeremy</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228922011-12-21T01:23:10Zyeban (Anurag Priyam)anurag08priyam@gmail.com
<ul></ul><p>On Tue, Dec 20, 2011 at 8:17 PM, Jeremy Bopp <a href="mailto:jeremy@bopp.net">jeremy@bopp.net</a> wrote:</p>
<blockquote>
<p>How about adding an attr_query method that otherwise works identically<br>
to attr_reader but creates a method with a &#39;?&#39; on the end of the given<br>
name instead?</p>
</blockquote>
<p>I would suggest the name <code>predicate</code> instead.</p>
<p>class Foo<br>
attr :bar<br>
predicate :bar<br>
end</p>
<p>Alternatively,</p>
<p>On Tue, Dec 20, 2011 at 7:33 PM, Yukihiro Matsumoto <a href="mailto:matz@ruby-lang.org">matz@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>The other option is removing &#39;?&#39; from instance variables. Â But as far as I remember no one seriously proposed the idea before, and we haven&#39;t got consensus.</p>
</blockquote>
<p>Use <code>attr</code>, but if the variable name passed to <code>attr</code> contains a &#39;?&#39;<br>
define a predicate corresponding to it too.</p>
<p>class Foo<br>
attr :bar?<br>
attr :baz<br>
end</p>
<p>f</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228932011-12-21T03:51:30Ztrans (Thomas Sawyer)
<ul></ul><p>@jeremy Been down that road, and it&#39;s not as clean as you might expect. You end up needing two methods e.g. <code>attr_query_reader</code> and <code>attr_query_accessor</code> (<code>attr_query_writer</code> would be essentially meaningless). Moreover, adding additional attr methods tends to be one of those &quot;Cambrian explosion&quot; deals --there are vast variations people have devised. Check out Rails for examples. So I don&#39;t think it&#39;s a good precedence for core Ruby. In fact I&#39;ve always thought it a bit unfortunate that #attr alone wasn&#39;t all we needed. </p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228942011-12-21T03:59:53Ztrans (Thomas Sawyer)
<ul></ul><p><a class="user active" href="/users/13">matz (Yukihiro Matsumoto)</a> In that case I would propose:</p>
<p>attr :foo?<br><br>
#=&gt; def foo?; @foo; end</p>
<p>attr_reader :foo? <br>
#=&gt; def foo?; @foo; end</p>
<p>attr_writer :foo?<br>
#=&gt; def foo=(x); @foo=(x); end</p>
<p>attr_accessor :foo? <br>
#=&gt; attr_reader :foo?; attr_writer :foo?</p>
<p>It&#39;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 &quot;predicate&quot; method and #attr_reader can define both? As to that later, I think it&#39;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.</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228952011-12-21T07:44:32ZEregon (Benoit Daloze)
<ul></ul><blockquote>
<p>So why not just allow: attr :foo?</p>
</blockquote>
<p>I agree. I know many people wish for that too.</p>
<blockquote>
<p>matz: The other option is removing &#39;?&#39; from instance variables. But as far as I remember no one seriously proposed the idea before, and we haven&#39;t got consensus.</p>
</blockquote>
<p>What do you mean by removing &#39;?&#39; from instance variables ?<br>
As you said, &#39;?&#39; is already forbidden in instance variable names.</p>
<blockquote>
<p>Thomas Sawyer: It&#39;s an open question as to whether #attr and/or #attr_reader should define the plan method too.</p>
</blockquote>
<p>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).</p>
<p>Also, since attr* :foo? does not conflict with current uses, I think it&#39;s fine to use the usual attr* methods.</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=228962011-12-21T08:19:13Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>attr_query :foo</p>
<p>Would be nice to add a query method ending in foo? to return the instance variable @foo.</p>
<p>I agree that changing current behaviour of attr or attr_reader or attr_writer would be bad.</p>
<p>But a new method &quot;attr_query :foo&quot; would be nice to have. (I don&#39;t agree with &quot;predicate :foo&quot;, <br>
but I could agree to attr_predicate :foo - I think it would be nice and consistent to include<br>
the attr_ part in such a method name.)</p>
<p>I also agree with matz on instance variables.</p>
<p>@foo? = &quot;hi&quot;</p>
<p>Does not read elegantly and thus should not be possible and not be changed.</p>
<p>But I also agree that there should be a very easy way to generate a <br>
query method ending in &quot;?&quot;.</p>
<p>It would make some code shorter.</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=229092011-12-22T04:27:58Zjballanc (Joshua Ballanco)jballanc@gmail.com
<ul></ul><p>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:</p>
<p>attr_accessor :foo, { :var =&gt; :bar, :getter =&gt; :is_barable? }, :baz</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=316732012-10-27T06:31:50Zko1 (Koichi Sasada)
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>next minor</i></li></ul><p>I changed target to next minor because no discussion on it.</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=350332012-12-24T03:43:37Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
I revisited this b/c in one of my projects it is much needed. To compensate, I created a special extension called (({attr_switch})),</p>
<pre>def attr_switch(name)
attr_writer name
module_eval %{
def #{name}?
@#{name}
end
}
end
</pre>
<p>But it has the problem that the (({#source_location})) for the method created is in attr_switch&#39;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. </p>
<p>file, line = *caller[0].split(&#39;:&#39;)[0..1]<br>
module_eval %{...}, file, line.to_i</p>
<p>But it didn&#39;t work.</p>
<p>So bringing this back to this feature request. I, for one, still would very much like this feature. Sometimes it&#39;s just much more convenient. And I&#39;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&#39;t even get it to work exactly the same).</p>
<p>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.</p>
<p>Given what I understand about the new ((<em>process</em>)) 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?<br>
=end</p>
Ruby trunk - Feature #5781: Query attributes (attribute methods ending in `?` mark)https://bugs.ruby-lang.org/issues/5781?journal_id=350422012-12-24T11:10:23Ztrans (Thomas Sawyer)
<ul></ul><p>=begin<br>
FYI,</p>
<p>file, line = *caller[0].split(&#39;:&#39;)[0..1]<br>
module_eval %{...}, file, line.to_i</p>
<p>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:</p>
<p>def attr_switch(name)<br>
file, line = *caller[0].split(&#39;:&#39;)[0..1]<br>
module_eval %{<br>
def #{name}=(x)<br>
@#{name}=x<br>
end<br>
def #{name}?<br>
@#{name}<br>
end<br>
}, file, line.to_i<br>
end</p>
<p>In any case, still would be better to have <code>attr_accessor :x?</code> work.<br>
=end</p>