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.
Wiebe C. (Guest)
on 2006-04-13 16: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.
Patrick H. (Guest)
on 2006-04-13 21:24
(Received via mailing list)
On 4/13/06, Wiebe C. <removed_email_address@domain.invalid> 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
Justin C. (Guest)
on 2006-04-13 21:57
(Received via mailing list)
Patrick H. 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
Mike F. (Guest)
on 2006-04-13 22:19
Justin C. 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).
Patrick H. (Guest)
on 2006-04-13 22:25
(Received via mailing list)
On 4/13/06, Mike F. <removed_email_address@domain.invalid> 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
Justin C. (Guest)
on 2006-04-13 22:34
(Received via mailing list)
Patrick H. 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
unknown (Guest)
on 2006-04-13 23:23
(Received via mailing list)
On Fri, 14 Apr 2006, Patrick H. wrote:

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

or just use

   attr_reader 'variable?'

;-)

-a
unknown (Guest)
on 2006-04-13 23:26
(Received via mailing list)
On Fri, 14 Apr 2006, Justin C. 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
Patrick H. (Guest)
on 2006-04-13 23:36
(Received via mailing list)
On 4/13/06, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> 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
unknown (Guest)
on 2006-04-14 00:03
(Received via mailing list)
On Fri, 14 Apr 2006, Patrick H. 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
Daniel B. (Guest)
on 2006-04-14 00:12
(Received via mailing list)
removed_email_address@domain.invalid 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
Wiebe C. (Guest)
on 2006-04-14 02:33
(Received via mailing list)
On Thursday 13 April 2006 19:23, Patrick H. 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.