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.
On 4/13/06, Wiebe C. [email protected] 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
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
On 4/13/06, Mike F. [email protected] 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. 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).
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
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
On 4/13/06, [email protected] [email protected] wrote:
define_method(“#{sym}?”) { !!instance_variable_get(“@#{sym}”) }
-a
be kind whenever possible… it is always possible.
That does not work for me, is there a trick?
pth
[email protected] 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
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.
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