Writing my own attr_* methods

D

David Brady

Hi All,

I frequently find myself creating member vars that contain a status flag
that want me to write a ? method. I know ruby can be tricked into
thinking it has an additional attr_ method, but off the top of my head I
can't think where I'd inject that method and how.

I figure some guru will know what to do to Kernel, Class or Object to
make this work:

----------8<----------8<----------8<----------
class Foo
attr_flag :bar, :baz
attr_flag_ro :qaz, :qux, :quux

def initialize(qaz, qux, quux)
@qaz=qaz; @qux=qux; @quux=quux
@bar = @baz = false
end
end

f = Foo.new(true, true, true)

f.bar = true
f.baz = true

puts "Yay!" if f.bar?
puts "Yay!" if f.baz?
puts "Yay!" if f.qaz?
puts "Yay!" if f.qux?
puts "Yay!" if f.quux?
----------8<----------8<----------8<----------

I don't know if attr_flag, attr_flag_ro is the right pairing--perhaps
attr_flag would be read only and if you want it to be settable you want
to call attr_accessor, etc.

Anyway, I'm not sure this is a perfect design, mostly I'm interested in
seeing *how* to write an attr_ method.

Thoughts?

Thanks!

-dB
 
T

Trans

Do some research on this, there's plenty of mail archives on the
subject. Or have a look at some Facets code like #attr_tester. (Hmm...
attr_flag actually seems like a better name). Anyway, here's a simple
example to get you started:

class Module
def attr_flag( x )
class_eval %{
def #{x}?
@#{x}
end
}
end
end

T.
 
M

Mark Hubbart

Hi All,

I frequently find myself creating member vars that contain a status flag
that want me to write a ? method. I know ruby can be tricked into
thinking it has an additional attr_ method, but off the top of my head I
can't think where I'd inject that method and how.

I figure some guru will know what to do to Kernel, Class or Object to
make this work:

----------8<----------8<----------8<----------
class Foo
attr_flag :bar, :baz
attr_flag_ro :qaz, :qux, :quux

def initialize(qaz, qux, quux)
@qaz=3Dqaz; @qux=3Dqux; @quux=3Dquux
@bar =3D @baz =3D false
end
end

f =3D Foo.new(true, true, true)

f.bar =3D true
f.baz =3D true

puts "Yay!" if f.bar?
puts "Yay!" if f.baz?
puts "Yay!" if f.qaz?
puts "Yay!" if f.qux?
puts "Yay!" if f.quux?
----------8<----------8<----------8<----------

I don't know if attr_flag, attr_flag_ro is the right pairing--perhaps
attr_flag would be read only and if you want it to be settable you want
to call attr_accessor, etc.

Anyway, I'm not sure this is a perfect design, mostly I'm interested in
seeing *how* to write an attr_ method.

Thoughts?

attr_ methods are instance methods of Class. They define the desired
accessor methods for that class when called:

class Class
def my_attr_reader(name)
define_method(name){instance_variable_get "@#{name}" }
end
def my_attr_writer(name)
define_method("#{name}=3D"){|value| instance_variable_set "@#{name}", v=
alue}
end
end

class Foo
my_attr_reader :bar
my_attr_writer :bar
end

f =3D Foo.new
f.bar =3D 23
f.bar #=3D=3D> 23

cheers,
Mark
 
C

Curt Hibbs

FreeRIDE does this in its Databus class by overriding method_missing.
Then when method_missing is called, we inspect the name of the method
that was trying to be invoked. If the name starts with "attr_" we do
the appropriate thing (we're simulating presence of arbitrary
attributes), otherwise we pass the medthod_missing call to the
superclass.

Curt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top