From: Sean O'Dell Date: 2004-07-01T05:07:52+09:00 Subject: Re: [ANN] arrayfields-3.0.0 On Wednesday 30 June 2004 12:32, Ara.T.Howard wrote: > 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)? I like your solution above where none of the arrayfields code goes directly into the Array class. Benefits: no overhead for Arrays that don't use the arrayfields feature, and the ability to work with other objects that aren't Array's but which have array-like functionality. Sean O'Dell