Would it make sense for attr_reader and friends to associate :foo? with
the attribute “@foo” ?
Consider the following code:
class C
attr_accessor :foo?
def initialize
@foo = true
end
end
c = C.new
p c.foo? # ==> false
c.send(“foo?=”, true)
p c.foo? # ==> true
p c.instance_variables # ["@foo"]
You can call the reader. You have to bend over backwards to call the
writer. You cannot access the attribute itself from within method
definitions. You can’t even see that it exists using #instance_variables.
Since the use of attr_accessor construct is so pointless in this case,
why not change the semantics:
“attr_reader :foo?” defines a method #foo? which gets
the value of @foo,
“attr_writer :foo?” defines a method #foo= which sets
the value of @foo.
This would not affect existing code because currently attr_* can’t
effectively be used on a symbol ending in “?”.
instance_variables works for me: I get
[“@foo?”, “@foo”]
(version 1.6.8, FreeBSD)
But I agree such an instance variable is not directly accessible (e.g.
@foo?.inspect fails with a parse error)
I guess the ? is being treated as part of the ? : operator rather than the
variable name:
@foo?.1:.2 # return .1 if @foo is true or .2 if @foo is false
Regards,
Brian.
···
On Sun, Jun 01, 2003 at 08:04:16AM +0900, Joel VanderWerf wrote:
Consider the following code:
class C
attr_accessor :foo?
def initialize
@foo = true
end
end
c = C.new
p c.foo? # ==> false
c.send(“foo?=”, true)
p c.foo? # ==> true
p c.instance_variables # [“@foo”]
You can call the reader. You have to bend over backwards to call the
writer. You cannot access the attribute itself from within method
definitions. You can’t even see that it exists using #instance_variables.
Hi,
instance_variables works for me: I get
[“@foo?”, “@foo”]
attr_(reader|writer|accessor) don’t create instance variable
itself. When the attribute is assinged, the variable will be
created.
I guess the ? is being treated as part of the ? : operator rather than the
variable name:
@foo?.1:.2 # return .1 if @foo is true or .2 if @foo is false
That’s right. ‘!’ and ‘?’ are allowed at the end of method
names, but not as a part of variable names.
···
At Sun, 1 Jun 2003 15:57:23 +0900, Brian Candler wrote:
–
Nobu Nakada