`attr_accessor` and predicate/question mark methods.

T

Tim Becker

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
 
D

dblack

Hi --

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
T

Tim Becker

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
David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
D

Daniel Berger

Tim said:
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

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.
 
R

Rob Sanheim

Tim said:
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

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.

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
 
D

Daniel Berger

Rob Sanheim wrote:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top