From: Justin Collins Date: 2006-04-14T02:57:08+09:00 Subject: Re: Question mark in attr_* --------------040005020804030009020601 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Patrick Hurley wrote: > On 4/13/06, Wiebe Cazemier wrote: > >> Hi, >> >> Methods which return booleans end with a question mark, by convention. But how >> about boolean attributes (attr_[reader, accessor, writer])? Saying >> "attr_reader :variable?" won't work. I find it rather strange when you have to >> access boolean attributes without the question mark, but the methods with the >> question mark. >> >> >> >> > > As ruby does not have data types, the onus is on the developer to > spell out that she/he wants such an accessor. So add this to your > program: > > class Module > def battr_reader(sym, *more) > battr_reader(*more) unless more.empty? > define_method("#{sym}?") { !!instance_variable_get("@#{sym}") } > end > end > > Then you can add a battr_reader to any class... > pth > > This could be pretty handy...but I don't understand why you have the double negation? > define_method("#{sym}?") { !!instance_variable_get("@#{sym}") } -Justin --------------040005020804030009020601--