Forum: Ruby Question mark in attr_*

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-13 14:42
(Received via mailing list)
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.
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-04-13 19:24
(Received via mailing list)
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
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (Guest)
on 2006-04-13 19:57
(Received via mailing list)
Patrick Hurley wrote:
>>
>    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
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2006-04-13 20:19
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).
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-04-13 20:25
(Received via mailing list)
On 4/13/06, Mike Fletcher <lemurific+rforum@gmail.com> wrote:
>
>
>
> --
> 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
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (Guest)
on 2006-04-13 20:34
(Received via mailing list)
Patrick Hurley wrote:
>> Maybe so the first ! coerces to a true or false, and the second flips it
>
> 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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-13 21:23
(Received via mailing list)
On Fri, 14 Apr 2006, Patrick Hurley wrote:

>>
> end
> Then you can add a battr_reader to any class...
> pth

or just use

   attr_reader 'variable?'

;-)

-a
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-13 21:26
(Received via mailing list)
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}") }

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
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-04-13 21:36
(Received via mailing list)
On 4/13/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> >>
> >   define_method("#{sym}?") { !!instance_variable_get("@#{sym}") }
>
> -a
> --
> 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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-13 22:03
(Received via mailing list)
On Fri, 14 Apr 2006, Patrick Hurley wrote:

>> 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
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel Berger (Guest)
on 2006-04-13 22:12
(Received via mailing list)
ara.t.howard@noaa.gov wrote:
>
> 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
A28088e0bbbdecd6663d554f7be1273f?d=identicon&s=25 Wiebe Cazemier (Guest)
on 2006-04-14 00:33
(Received via mailing list)
On Thursday 13 April 2006 19:23, Patrick Hurley wrote:

>
> Then you can add a battr_reader to any class...
> pth

I will give this a go tomorrow (it's bedtime now...). Should work pretty
well.
Thanks.
This topic is locked and can not be replied to.