From: "Ara.T.Howard" Date: 2004-07-01T04:32:53+09:00 Subject: Re: [ANN] arrayfields-3.0.0 On Thu, 1 Jul 2004, Sean O'Dell wrote: > Ara, any chance of getting arrayfields to not modify the Array methods but > instead simply add an accessor to Array which returns an object that > encapsulates this new functionality? > > So, instead of saying: > > row = ['bob', 30] > row.fields = ['name', 'age'] > row['name'] #=> 'bob' > > you would say something like: > > row = ['bob', 30] > row.arrayfields.fields = ['name', 'age'] > row.arrayfields['name'] #=> 'bob' > > My thinking is that if arrayfields functionality were contained in its own > object that linked back to a parent array, you could create arrayfield > objects and attach them to other "array-like" objects, such as database rows > and so on. > > But also, it might prevent problems that arise later from taking over Array's > methods where people want to use both arrayfields, and regular Array > functionality. well - theoretically you lose zero Array functionality since ArrayFields methods are used if, and only if, the idx paramter is a String or Symbol which would never be done with a normal Array. that being said: jib:~/shared > cat a.rb require 'arrayfields' class FieldedArray attr :fieldset def initialize fields, array @a = array self.fields = fields end def method_missing(meth, *args, &block) @a.send(meth, *args, &block) end def fields= fields extend ArrayFields unless defined? @fieldset @fieldset = if ArrayFields::FieldSet === fields fields else ArrayFields::FieldSet.new fields end end attr_reader :fieldset def fields @fieldset and @fieldset.fields end end fa = FieldedArray.new %w(zero one two), [0, 1, 2] fa.each_with_field{|e, f| puts "#{ f } : #{ e }"} jib:~/shared > ruby a.rb zero : 0 one : 1 two : 2 i can't bloody believe that worked - go matz! what'ya think of adding the above to 3.3.0 (along with gem spec)? -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 ===============================================================================