From: Austin Ziegler Date: 2003-11-05T13:09:40+09:00 Subject: Re: Managing metadata about attribute types On Wed, 5 Nov 2003 09:38:16 +0900, Simon Kitching wrote: > I'm porting the Apache Jakarta Commons Digester (written in Java) > to Ruby at the moment. This module processes xml in a rules-based > manner. It is particularly useful for handling complex xml > configuration files. You may want to note xml-configfile in addition to XMLDigester that James Britt mentioned. http://raa.ruby-lang.org/list.rhtml?name=xml-configfile > Finding attributes on a Ruby class is simple (just look for > "attr=" methods). Unfortunately, determining what object types it > is valid to assign to that attribute is not so simple... Aside from Ryan Pavlik's StrongTyping module, I'm not sure that this is absolutely necessary. See below for a bit more information. > I was wondering if there were any other Ruby projects which have > faced this problem and come up with solutions? I would rather > steal a solution than invent one :-) I frankly don't see a reason to worry about this. > Example of problem: > > Input xml is: > > > > > > // java > class StockItem { > public void setName(String name) {....} > public void setCost(float cost) {....} > } > # Ruby > class StockItem > attr_accessor :name > attr_accessor :cost > end Why not do: class StockItem attr_accessor :name # Defaults to String attr_reader :cost # Returns cost def cost=(x) @cost = x.to_f end This way, you don't have to care what the appropriate type is -- your type worries about it. -austin -- austin ziegler * austin@halostatue.ca * Toronto, ON, Canada software designer * pragmatic programmer * 2003.11.04 * 22.46.52