/^attr(?:_reader|_writer)*$/ - possible RCR?

does anyone else think that having attr_* keep track of the attributes might
be helpful? i’ve done it by hand many times. i’m thinking something like
this might be generally useful:

attr.rb
====CUT====
class Module
private
alias_method :__attr_reader, :attr_reader
alias_method :__attr_writer, :attr_writer
alias_method :__attr, :attr
def attr_reader(*symbols)
__attr_reader *symbols
(@reader_attributes ||= ).push *symbols
end
def attr_writer(*symbols)
__attr_writer *symbols
(@writer_attributes ||= ).push *symbols
end
def attr symbol, writable = false
__attr symbol, writable
(@reader_attributes ||= ).push symbol
(@writer_attributes ||= ).push symbol if writable
end
public
def attributes type = nil
case type.to_s
when %r/read|get/
@reader_attributes.clone
when %r/writ|set/
@writer_attributes.clone
else
@reader_attributes | @writer_attributes
end
end
end

if $0 == FILE
class Foo
attr_reader :foo
attr_writer :bar
attr :foobar
attr :barfoo, true
def not_an_attribute; end
end

p Foo.attributes          # -> [:foo, :foobar, :barfoo, :bar]
p Foo.attributes :reader  # -> [:foo, :foobar, :barfoo]
p Foo.attributes :writer  # -> [:bar, :barfoo]

end
====CUT====

here’s an example where/why i’ve used it:

class CannotMarshal < RBTree



def _dump depth
Marshal.dump {:data => to_hash, :attributes => class.attributes(:reader).sort.map{|a| send a}}
end
class << self
def _load string
cm = CannotMarshal.new
loaded = Marshal.load string
cm.update loaded[:data]
cm.class.attributes(:writer).sort.map{|a| cm.send a, loaded[:attributes].shift}
cm
end
end
end

the above requires a writer for every reader, but you get the idea…

-a

···

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

ahoward wrote:

does anyone else think that having attr_* keep track of the attributes might
be helpful? i’ve done it by hand many times. i’m thinking something like
this might be generally useful:

Could you use #instance_variables for this, or do you need to
distinguish those that have only readers from those that have only writers?

exactly - though perhaps not in my example.

-a

···

On Tue, 17 Jun 2003, Joel VanderWerf wrote:

Could you use #instance_variables for this, or do you need to
distinguish those that have only readers from those that have only writers?

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

“ahoward” ahoward@fsl.noaa.gov schrieb im Newsbeitrag
news:Pine.LNX.4.53.0306162318250.21200@eli.fsl.noaa.gov

Could you use #instance_variables for this, or do you need to
distinguish those that have only readers from those that have only
writers?

exactly - though perhaps not in my example.

Another option would be to follow the Java Properties approach, i.e.,
detect properties from a naming pattern. Unfortunately for ruby this
means that one can only detect attributes that have reader and writer.
Not very useful, I guess.

Then your approach might be better. I didn’t inspect your code, but did
you provide an option to manually register attributes afterwards? Could
be useful when inheriting classes that define attributes not via attr.

Cheers

robert
···

On Tue, 17 Jun 2003, Joel VanderWerf wrote: