From: "Ara.T.Howard" Date: 2004-06-30T06:37:56+09:00 Subject: Re: [ANN] arrayfields-3.0.0 On Wed, 30 Jun 2004, Joel VanderWerf wrote: > --- > module AccessibleIndex > def index_accessor(h) > h.each do |sym, idx| > define_method sym do > self[idx] > end > define_method "#{sym}=" do |val| > self[idx] = val > end > end > end > end > > a = [0,1,2,3,4,5] > class << a > extend AccessibleIndex > index_accessor :x => 3 > end > > a.x = "three" > p a # ==> [0, 1, 2, "three", 4, 5] > --- > > But this approach doesn't help with values_at and the like. cool. your way is perhaps safer. ;-) the reason i wrote this in the first place was that i was doing alot of postgresql work and it was returning these huge result sets that i would then write code that looked like: if tuple[17] =~ pat or tuple[9].to_i < 42 ... end and i was reading my own code thinking - huh? so then i started doing: res = pgconn.exec sql tuples = res.result fields = res.fields tuples = tuples.collect do |tuple| h = {} fields.each do |field| h[field] = tuple.shift end h end this is NOT good if tuple.size == 42000!! finally i wrote the arrayfields module. it still requires work: res = pgconn.exec sql tuples = res.result fields = res.fields tuples.each{|t| t.fields = fields} but now all 42000 tuples SHARE a copy of fields for doing their lookups - i still have to iterate over all of em - but i don't need to create any new objects and my code new reads like: if tuple['name'] =~ pat or tuple['age'].to_i < 42 ... end which is a lot nicer. in any case it's just one of those things that i ended up using alot. i'd like to re-write it in C but don't have the time right now... cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================