`attr_accessor` and predicate/question mark methods

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

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

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

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.

On 11/29/06, Daniel B. [email protected] 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

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