[TIP] Working with columns that end with "?", like "male?"

Chris Bloom wrote the following on 04.10.2007 23:24 :


a.first_place? = true       

SyntaxError: compile error (irb):69: syntax error, unexpected '=', expecting $end a.first_place? = true                 ^         from (irb):69         from :0    This tries to send "first_place?=" to the a object.

My guess is that it is Ruby that prevents the ? from being part of the method name unless it is the last character.

Nothing ActiveRecord can do about it...


I think the real question is why on earth would you want to have those column names in the first place? If you have to work with a legacy db, sure, but I'd never ever ever do that in a migration myself.

And if I did have a legacy db with that schema, I'd just write some sensible methods:

def male=(val)   write_attribute :male?, val end

def male   read_attribute :male? end

def male?   !!male end

Normal assignment, raw attribute, and predicate...the Rails Way.