# \`attr\_accessor\` and predicate/question mark methods

**URL:** <https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655>\
**Category:** Ruby\
**Created:** [November 29, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655 "2006-11-29T16:05:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/1 "2006-11-29T16:05:29Z")

</div>

Hi,  
I just stumbled across this weird behaviour and I’m puzzled that I’ve  
not noticed it before. If I use `attr_accessor` to define a predicate  
method, i.e. one ending in a question mark, the following happens:

class Test  
attr\_accessor :weird?  
end

t = new Test  
t.weird? =\> nil  
t.weird?=true =\> SyntaxError: compile error

Which is only mildly confusing, considering it’s implied somewhere in  
the pickaxe book that bangs or question marks are only allowed at the  
end of method names. In fact:

class Test2  
def weird?= val  
end  
end

fails with `SyntaxError` as well. What’s baffling me is that  
`attr_accessor`'s is capable of adding the method `weird?=`, which  
can be verified calling `methods`:

t.methods =\> [… “weird?=” …]

The method is there, only calling it causes the `SyntaxError`… Am I  
missing something entirely obvious here?  
-tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/2 "2006-11-29T16:23:33Z")

</div>

Hi –

On Thu, 30 Nov 2006, Tim B. wrote:

> Hi,  
> I just stumbled across this weird behaviour and I’m puzzled that I’ve  
> not noticed it before. If I use `attr_accessor` to define a predicate  
> method, i.e. one ending in a question mark, the following happens:
> 
> class Test  
> attr\_accessor :weird?  
> end

I get:

irb(main):002:0\> class Test  
irb(main):003:1\> attr\_accessor :weird?  
irb(main):004:1\> end  
NameError: invalid attribute name `weird?’

What version of Ruby are you using?

David

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/3 "2006-11-29T16:35:37Z")

</div>

> What version of Ruby are you using?  
> A rather oldish 1.8.2, I just realized:

> ruby -version  
> ruby 1.8.2 (2004-12-25) [i686-linux]

So this is in fact most likely a fixed bug. Sorry for not googling  
this, but try searching for ?=

Good to know I’m not insane, just lazy. I’ve checked 1.8.5 on this  
setup, and it worked.

Thanks!  
-tim

rb(main):001:0\> class Test  
irb(main):002:1\> attr\_accessor :weird?  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> exit

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/4 "2006-11-29T16:45:40Z")

</div>

Tim B. wrote:

> t.weird? =\> nil
> 
> fails with `SyntaxError` as well. What’s baffling me is that  
> `attr_accessor`'s is capable of adding the method `weird?=`, which  
> can be verified calling `methods`:
> 
> t.methods =\> [… “weird?=” …]
> 
> The method is there, only calling it causes the `SyntaxError`… Am I  
> missing something entirely obvious here?  
> -tim

This was a bug that was “fixed” in a 1.8.5. In 1.8.5 trying to do  
‘attr\_accessor :weird?’ raises a NameError.

I had voted that doing ‘attr\_accessor :weird?’ should create a “weird?”  
and “weird=” method, but I was shot down by programmers who were more  
interested in being correct than useful (am I bitter? nah). See  
ruby-core:5796 and following for more information.

Regards,

Dan

PS - My apologies if this is a double post - Google server flaked out.

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/5 "2006-11-29T22:49:12Z")

</div>

On 11/29/06, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > t = new Test  
> > end
> 
> Dan
> 
> PS - My apologies if this is a double post - Google server flaked out.

Hmm, I just went through the thread on ruby-core, and I didn’t see  
anyone really shoot you down. There was some debate, but the last  
thing Matz said was “I’m not sure”, so maybe its still an option? Or  
was there a later thread that officially shot it down?

- Rob

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [November 29, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/attr-accessor-and-predicate-question-mark-methods/77655/6 "2006-11-29T23:11:21Z")

</div>

Rob S. wrote:

> > This was a bug that was “fixed” in a 1.8.5. In 1.8.5 trying to do  
> > ‘attr\_accessor :weird?’ raises a NameError.
> > 
> > I had voted that doing ‘attr\_accessor :weird?’ should create a “weird?”  
> > and “weird=” method, but I was shot down by programmers who were more  
> > interested in being correct than useful (am I bitter? nah). See  
> > ruby-core:5796 and following for more information.

> Hmm, I just went through the thread on ruby-core, and I didn’t see  
> anyone really shoot you down. There was some debate, but the last  
> thing Matz said was “I’m not sure”, so maybe its still an option? Or  
> was there a later thread that officially shot it down?

The current 1.8.5 behavior shows which direction Matz chose so, yes, I  
was shot down (implicitly rather than explicitly - another cause for  
annoyance, actually). There may have been followup in ruby-devel, but  
I don’t follow that list (since it’s mostly Japanese). I doubt there’s  
any going back now, but you’re certainly free to try.

Regards,

Dan
