From: Curt Hibbs Date: 2005-11-10T05:06:01+09:00 Subject: Re: Writing my own attr_* methods 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 On 11/9/05, David Brady wrote: > 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 > > -- > David Brady > ruby_talk@shinybit.com > C++ Guru. Ruby nuby. Apply salt as needed. > > > >