Chris said:
attr_reader
foo?)
def initialize
@foo?=true
end
[...] but I know the above won't work because
you used attr_reader which will create a getter, but no setter for foo?.
What? You don't need a public setter function to set the value of an
instance variable. That's the whole point of attr_reader
For example, the following code works just fine:
class Bar
attr_reader
foo)
def initialize
@foo=true
end
end
bar = Bar.new
puts bar.foo
as does this code:
class Bar
def initialize
@foo=true
end
def foo?
return @foo
end
end
bar = Bar.new
puts bar.foo?
My only point is that if I could name an instance variable @foo? then
the code quoted at the top of this post would work, and be slightly less
verbose.
I suppose it's not allowed because you can define a method named foo?
but not one named foo?= (thus enforcing the idea that a method ending in
a question mark is for querying a boolean aspect of an instance, and not
for setting one) and you might want to be able to publicly set the state
of a @foo? variable.