[Note: parts of this message were removed to make it a legal post.]
On Mon, Apr 12, 2010 at 12:35 AM, Albert Schlef <>wrote:
> Albert Schlef wrote:
> > Thank you both. I wanted to make sure there's no built-in way to do
> > this.
>
> Hey, I've just discovered that 'ri' tells me that Module has an
> 'attr_reader?' method that creates a question-mark attribute.
>
> Problem is, 'ri' displays methods from everything I've installed on my
> system, so I can't know if 'attr_reader?' is provided by Ruby itself or
> by some wacky gem.
> --
> Posted via http://www.ruby-forum.com/.
>
>
Doesn't work for me, so probably from some gem.
You could do something like this:
class Module
def attr_accessor?(*methods)
methods.each do |method|
ivar = "@#{method}"
define_method("#{method}=") { |value| instance_variable_set ivar ,
value }
define_method("#{method}?") { !!instance_variable_get(ivar) }
end
end
end
class Example
attr_accessor? :abc
end
e = Example.new
e.abc = true
e.abc? # => true
e.abc = false
e.abc? # => false
e.abc = 1
e.abc? # => true
e.abc = nil
e.abc? # => false
e.abc = :adsf
e.abc? # => true
There was also a ruby quiz about attr methods
http://rubyquiz.com/quiz67.html And there is a gem based off of it
http://github.com/ahoward/fattr
I don't know where yours came from, though.