https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?14253458252014-02-03T15:05:38ZRuby Issue Tracking SystemRuby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=448962014-02-03T15:05:38ZJoshua Ballancojballanc@gmail.com
<ul></ul><p><em>bump</em></p>
<p>Would be nice if we could get this in 2.2 (if someone could update the target version, I&#39;d appreciate it).</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=448972014-02-03T15:13:24ZBenoit Daloze
<ul></ul><pre>private *attr_reader(:images, :key_map, :window, :screen_manager, :animations)
</pre>
<p>does not look so nice to me (the parens and the explicit splat).<br>
Maybe #private should accept an Array of Symbols so:</p>
<pre>private attr_reader :images, :key_map, :window, :screen_manager, :animations
</pre>
<p>If there are so much &quot;:&quot;, it might also be worth using %i:</p>
<pre>private attr_reader %i[images key_map window screen_manager animations]
</pre>
<p>but then attr_reader would also need to accept an Array of Symbols.</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=448982014-02-03T18:44:01ZTsuyoshi Sawadasawadatsuyoshi@gmail.com
<ul></ul><p>What is the point of defining a private accessor method? You can directly refer to the instance variables without using accessors.</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=449052014-02-04T05:22:31ZAvdi Grimmavdi@avdi.org
<ul></ul><p>There are a number of reasons I use them, but the most obvious is that a<br>
misspelled accessor method will raise NoMethod error, whereas a misspelled<br>
@ivar reference will silently return nil.</p>
<p>On Mon, Feb 3, 2014 at 1:44 PM, <a href="mailto:sawadatsuyoshi@gmail.com">sawadatsuyoshi@gmail.com</a> wrote:</p>
<blockquote>
<p>Issue <a href="https://bugs.ruby-lang.org/issues/9453" class="issue tracker-2 status-6 priority-4 priority-default closed" title="Return symbols of defined methods for `attr` and friends (Rejected)">#9453</a> has been updated by Tsuyoshi Sawada.</p>
<p>What is the point of defining a private accessor method? You can directly<br>
refer to the instance variables without using accessors.</p>
<hr>
<p>Feature <a href="https://bugs.ruby-lang.org/issues/9453" class="issue tracker-2 status-6 priority-4 priority-default closed" title="Return symbols of defined methods for `attr` and friends (Rejected)">#9453</a>: Return symbols of defined methods for <code>attr</code> and friends<br>
<a href="https://bugs.ruby-lang.org/issues/9453#change-44898">https://bugs.ruby-lang.org/issues/9453#change-44898</a></p>
<ul>
<li>Author: Joshua Ballanco</li>
<li>Status: Open</li>
<li>Priority: Normal</li>
<li>Assignee:</li>
<li>Category: core</li>
</ul>
<a name="-Target-version"></a>
<h2 >* Target version:<a href="#-Target-version" class="wiki-anchor">&para;</a></h2>
<p>With Ruby 2.1 returning a symbol from <code>def</code> and <code>define_method</code>, that<br>
leaves <code>attr</code>, <code>attr_reader</code>, <code>attr_writer</code>, and <code>attr_accessor</code> as ways to<br>
define methods that still return nil. This is unfortunate, because it<br>
prevents the use of method decorators developed to work with <code>def</code> from<br>
also working with the <code>attr*</code> methods. Because these mechanisms can define<br>
more than one method, the return values would need to be arrays of symbols.</p>
<p>For an example of how this could be useful in real-world code, consider<br>
this sample from James Edward Gray II&#39;s Warehouse Keeper example (<br>
<a href="https://github.com/JEG2/warehouse_keeper):">https://github.com/JEG2/warehouse_keeper):</a></p>
<pre>attr_reader :images, :key_map, :window, :screen_manager, :animations
private :images, :key_map, :window, :screen_manager, :animations
</pre>
<p>if <code>attr_reader</code> returned symbols, then this could be simplified to:</p>
<pre>private *attr_reader(:images, :key_map, :window, :screen_manager,
</pre>
<p>:animations)</p>
<p>I&#39;ve attached a patch that implements this change and includes a few<br>
tests. For those who use git, I&#39;ve also submitted this as a pull request<br>
here: <a href="https://github.com/ruby/ruby/pull/517">https://github.com/ruby/ruby/pull/517</a></p>
<p>---Files--------------------------------<br>
attr_rv.patch (3.23 KB)</p>
<h2></h2>
<p><a href="http://bugs.ruby-lang.org/">http://bugs.ruby-lang.org/</a></p>
</blockquote>
<p>-- <br>
Avdi Grimm<br>
<a href="http://avdi.org">http://avdi.org</a></p>
<p>I only check email twice a day. to reach me sooner, go to<br>
<a href="http://awayfind.com/avdi">http://awayfind.com/avdi</a></p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=449142014-02-05T00:27:11ZJoshua Ballancojballanc@gmail.com
<ul></ul><p>Tsuyoshi Sawada wrote:</p>
<blockquote>
<p>What is the point of defining a private accessor method? You can directly refer to the instance variables without using accessors.</p>
</blockquote>
<p>The example I gave was just one case of &quot;code in the wild&quot; that would benefit from this change. Undoubtedly, as more people begin to take advantage of the ability to build method decorators (now that method definitions return something other than <code>nil</code>), having the attr* family of methods return something meaningful will mean that they can also benefit from these decorators.</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=459662014-03-27T17:13:15ZJoshua Ballancojballanc@gmail.com
<ul></ul><p>It&#39;s been almost 2 months...any chance we could get a comment on this from the core team?</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=459812014-03-28T15:55:51ZUsaku NAKAMURAusa@garbagecollect.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>Yukihiro Matsumoto</i></li></ul> Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=459912014-03-29T14:58:15ZYukihiro Matsumotomatz@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Rejected</i></li></ul><p>I am not positive. The example</p>
<p>private *attr_reader(:images, :key_map, :window, :screen_manager,:animations)</p>
<p>is not really intuitive. Besides that, you can define private_attr_reader, etc. by yourself.<br>
In that case, the code will be</p>
<p>private_attr_reader :images, :key_map, :window, :screen_manager,:animations</p>
<p>and looks much better.</p>
<p>Matz</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=461862014-04-11T19:48:21ZThomas Sawyer
<ul></ul><p>Letting #private accept an Array seems more preferable then adding yet another <del>method</del> slew of methods: <code>private_attr_writer</code>, <code>private_attr_accessor</code>, <code>protected_attr_reader</code>, <code>protected_attr_writer</code>, <code>protected_attr_accessor</code>, ...</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=461882014-04-12T01:31:11ZYukihiro Matsumotomatz@ruby-lang.org
<ul></ul><p>Thomas, private method that accept an array for methods names would be an interesting idea worth discussion.<br>
But it is different issue.</p>
<p>Matz.</p>
Ruby trunk - Feature #9453: Return symbols of defined methods for `attr` and friendshttps://bugs.ruby-lang.org/issues/9453?journal_id=482632014-08-08T22:38:43ZAndrew Lazarusnerdrew@gmail.com
<ul></ul><p>Chaining the two suggestions leads to a nice syntax (in my opinion):</p>
<p><code>private attr_reader :name, :address, :etc</code></p>
<p><code>private</code> needs to accept an array and <code>attr_*</code> needs to return its list (as an array) of arguments.</p>