From: Chad Fowler Date: 2003-11-05T13:09:01+09:00 Subject: Re: Managing metadata about attribute types On Wed, 5 Nov 2003, Simon Kitching wrote: # Hi, # # 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. # # However some of the very nice features of this module depend upon being # able to introspect a class to find what attributes it has, and what # their datatypes are. # # 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... # I'm not exactly answering your question here, but I'd like to offer some words of caution... Be careful about relying on what "type" of object you've got (I'll avoiding going into detailed discussion of the fact that "type" and "class" are not equivalent). An object's class doesn't guarantee much of anything in Ruby. Javaisms may not apply: class String undef :upcase, :downcase, :chomp, :chop, :capitalize end naive_method("is this really a string?") or even better: class A end a = A.new def a.is_a?(x) true end As you can see, any code that that relies on this kind of "type checking" in Ruby is naive. Worse than that, the desire to shoe-horn Ruby into Java-like "strictness" can blind the user into missing the point, and therefore the full benefit, of what Ruby has to offer. As I said, I haven't actually addressed your problem here. If it were me, I would be looking for (or writing) something that generates code based on an XML Schema. What you're really looking for is convenience (as opposed to "type safety"). You want to be able to add 1 + 1 and not end up with 11. You could easily accomplish this via some tedious coding or you could generate the tedious code. By tedious, I mean things like this: class Person def age=(how_old) @age = Integer(how_old) end end Ruby being as dynamic as it is, it would be pretty easy to dream up a scheme to wrap existing classes with filters that could do this kind of conversion for you. And, of course, you could generate the filters. And now, the cold medication sets in.... Good night, Chad