From: "Ara.T.Howard" Date: 2005-09-15T00:20:04+09:00 Subject: Re: Sets, uniqueness not unique. > OK, it seems I'm thinking along the right lines now. Here is what I did in > the end: < snip code > > I was particularly pleased to be able not to repeat the FIELDS, by means of > attr_accessor, and that the idea of doing (self.class)::FIELDS actually > worked. i do alot of that type of thing and use my traits lib a lot for it - it can make it pretty compact. for instance: harp:~ > cat a.rb require 'md5' require 'traits' module TraitCollection def initialize(*list) list = [ list ].flatten wt.each_with_index do |t,i| v = list[i] or raise ArgumentError, "no <#{ t }> given in <#{ list.inspect }>!" send t, v end end def to_s (rt.map{|t| [t, send(t)].join '='}. << "hash=#{ hash }").inspect end alias inspect to_s def hash rt.inject(::MD5::new()){|d,m| d << send(m)}.hexdigest.hex end def eql?(other) rt.inject(true){|b,v| b && (send(v) == other.send(v)) rescue false} end def wt; self::class::writer_traits; end def rt; self::class::reader_traits; end def self::included other super; class << other; class << self; alias [] new; end; end end end class Student include TraitCollection traits *%w( forename surname birth_dt picture coll_status ) end class Course include TraitCollection traits *%w( aos_code dept_code aos_type full_desc ) end require 'set' sset = Set::new s0, s1 = Student[%w( a b c d e )], Student[%w( f g h i j )] sset.add s0 42.times{ sset.add s1 } p sset cset = Set::new c0, c1 = Course[%w( a b c d )], Course[%w( e f g h )] cset.add c0 42.times{ cset.add c1 } p cset harp:~ > ruby a.rb # # cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================