https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112009-08-24T12:30:43ZRuby Issue Tracking SystemRuby master - Bug #1983: Struct doesn't define instance methods for valid method nameshttps://bugs.ruby-lang.org/issues/1983?journal_id=53792009-08-24T12:30:43Ztmat (Tomas Matousek)tomas.matousek@microsoft.com
<ul></ul><p>=begin<br>
Since</p>
<p>irb(main):012:0> class C<br>
irb(main):013:1> attr_accessor :foo?<br>
irb(main):014:1> end<br>
NameError: invalid attribute name `foo?'</p>
<p>I would rather think that the same error should be raised by Struct.new.</p>
<p>BTW, even more interesting names for attributes are operator names:</p>
<p>irb(main):007:0> Struct.new(:===, :+, :-)[1,2,3]<br>
=> #<struct #<a href="Class:0x392b154" class="external">Class:0x392b154</a> :====1, :+=2, :-=3></p>
<p>=end</p> Ruby master - Bug #1983: Struct doesn't define instance methods for valid method nameshttps://bugs.ruby-lang.org/issues/1983?journal_id=53802009-08-24T13:24:37Zbitsweat (Jeremy Daer)jeremydaer@gmail.com
<ul></ul><p>=begin<br>
Good point; struct is consistent with attr_*. Both use the same low-level check: (rb_is_local_id(id) || rb_is_const_id(id))</p>
<p>I'd like attr_reader :foo? to work too. It's a common convention for booleans. And for Struct, this is a nice way to stub out a duck-typed object.<br>
=end</p> Ruby master - Bug #1983: Struct doesn't define instance methods for valid method nameshttps://bugs.ruby-lang.org/issues/1983?journal_id=73762009-12-21T11:56:44Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>=begin</p>
<p>=end</p> Ruby master - Bug #1983: Struct doesn't define instance methods for valid method nameshttps://bugs.ruby-lang.org/issues/1983?journal_id=85192010-03-02T22:42:07Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>=begin<br>
Hi Jeremy,</p>
<blockquote>
<p>By why not generate the reader and skip the setter, then?</p>
</blockquote>
<p>In <a href="https://blade.ruby-lang.org/ruby-core/4404">[ruby-core:4404]</a>, matz decided to prohibit such a field name once.<br>
And in <a href="https://blade.ruby-lang.org/ruby-core/4577">[ruby-core:4577]</a>, he relaxed the restriction:</p>
<blockquote>
<p>having field names with "?" can be valid if field<br>
accessor is disabled for such cases.</p>
</blockquote>
<p>So it is currently intended, not a bug. I close this ticket.</p>
<p>You can re-register this as Feature ticket.<br>
If you do, please elaborate the motivation example.</p>
<blockquote>
<p>I'd like attr_reader :foo? to work too. It's a common convention for booleans.</p>
</blockquote>
<p>We cannot define @foo? even by using reflection:</p>
<p>instance_variable_set(:"@foo?", 42)<br>
#=> `@foo?' is not allowed as an instance variable name (NameError)</p>
<p>It is never a common convention.</p>
<p>IMO, if such a field name is allowed, we would next want to use foo?=<br>
and @foo?. I think Ruby design is sane because it limits ourselves<br>
from such earthly desires.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a><br>
=end</p> Ruby master - Bug #1983: Struct doesn't define instance methods for valid method nameshttps://bugs.ruby-lang.org/issues/1983?journal_id=85552010-03-03T14:20:21Zbitsweat (Jeremy Daer)jeremydaer@gmail.com
<ul></ul><p>=begin<br>
Thanks Yusuke. It's a reasonable restriction, considering my only motivation is to use Struct to make stubs for testing other code.<br>
=end</p>