From: Daniel Berger Date: 2006-11-30T00:45:11+09:00 Subject: Re: `attr_accessor` and predicate/question mark methods. Tim Becker 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 > > 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.