Question mark in attr_*

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

···

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.

Patrick Hurley 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

···

On 4/13/06, Wiebe Cazemier <halfgaar@gmx.net> wrote:

or just use

   attr_reader 'variable?'

:wink:

-a

···

On Fri, 14 Apr 2006, Patrick Hurley wrote:

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
Then you can add a battr_reader to any class...
pth

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

I will give this a go tomorrow (it's bedtime now...). Should work pretty well.
Thanks.

···

On Thursday 13 April 2006 19:23, Patrick Hurley wrote:

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

Justin Collins wrote:
[...]

This could be pretty handy...but I don't understand why you have the
double negation?

   define_method("#{sym}?") { !!instance_variable_get("@#{sym}") }

Maybe so the first ! coerces to a true or false, and the second flips it
back to the proper value (so you can set it with whatever value, but the
accessor will always return true or false).

···

--
Posted via http://www.ruby-forum.com/\.

if you like having this check out traits - it does this automatically

   require 'traits'

   class C
     trait 't'
   end

   c = C.new

   c.t = 42
   p c.t #=> 42
   p c.? #=> true

   c.t 42 # same as c.t= 42

regards.

-a

···

On Fri, 14 Apr 2006, Justin Collins wrote:

This could be pretty handy...but I don't understand why you have the double negation?

   define_method("#{sym}?") { !!instance_variable_get("@#{sym}") }

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

That does not work for me, is there a trick?

pth

···

On 4/13/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Fri, 14 Apr 2006, Patrick Hurley wrote:

> 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

or just use

   attr_reader 'variable?'

:wink:

-a
--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

Exactly -- you could remove it all together, but this way the method
always returns true/false regardless of the value of the property.

pth

···

On 4/13/06, Mike Fletcher <lemurific+rforum@gmail.com> wrote:

Justin Collins wrote:
[...]
> This could be pretty handy...but I don't understand why you have the
> double negation?
>> define_method("#{sym}?") { !!instance_variable_get("@#{sym}") }

Maybe so the first ! coerces to a true or false, and the second flips it
back to the proper value (so you can set it with whatever value, but the
accessor will always return true or false).

--
Posted via http://www.ruby-forum.com/\.

wow. you are right. it fails silently on 1.8.1 and throws an error on 1.8.4.

don't know what i was thinking!?

now that i think about it it was one of the reasons i wrote traits!

very, sorry for noise.

-a

···

On Fri, 14 Apr 2006, Patrick Hurley wrote:

or just use

   attr_reader 'variable?'

--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama

Patrick Hurley wrote:

···

On 4/13/06, Mike Fletcher <lemurific+rforum@gmail.com> wrote:
  

Justin Collins wrote:
[...]
    

This could be pretty handy...but I don't understand why you have the
double negation?
      

   define_method("#{sym}?") { !!instance_variable_get("@#{sym}") }
        

Maybe so the first ! coerces to a true or false, and the second flips it
back to the proper value (so you can set it with whatever value, but the
accessor will always return true or false).

--
Posted via http://www.ruby-forum.com/\.

Exactly -- you could remove it all together, but this way the method
always returns true/false regardless of the value of the property.

pth
  

Okay, makes sense.

Thanks,

Justin

or just use

   attr_reader 'variable?'

wow. you are right. it fails silently on 1.8.1 and throws an error on 1.8.4.

don't know what i was thinking!?

now that i think about it it was one of the reasons i wrote traits!

very, sorry for noise.

-a

Unfortunately, it looks like they decided to error out rather than take a DWIM approach:

# attrtest.rb
class Foo
    attr_accessor :bar?, :baz!
end

p Foo.instance_methods(false)

>ruby -v
ruby 1.8.2 (2004-12-25) [sparc-solaris2.10]
>ruby attrtest.rb
["bar?", "baz!=", "bar?=", "baz!"]

>/opt/bin/ruby -v
ruby 1.8.4 (2005-12-24) [sparc-solaris2.10]
>/opt/bin/ruby attrtest.rb
attrtest.rb:2:in `attr_accessor': invalid attribute name `bar?' (NameError)
         from attrtest.rb:2

Regards,

Dan

···

ara.t.howard@noaa.gov wrote:

On Fri, 14 Apr 2006, Patrick Hurley wrote: