Question mark in attr_*

FYI, I brought this issue up at
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/5796, where
attr_ methods did what you meant with regards to '?' and '!'.

Regards,

Dan

···

-----Original Message-----
From: Patrick Hurley [mailto:phurley@gmail.com]
Sent: Thursday, April 13, 2006 11:23 AM
To: ruby-talk ML
Subject: Re: Question mark in attr_*

On 4/13/06, Wiebe Cazemier <halfgaar@gmx.net> 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