NAME arrayfields.rb URIS http://www.codeforpeople.com/lib/ruby/ http://rubyforge.org/projects/codeforpeople/ SYNOPSIS require 'arrayfields' a = Arrayfields.new :k, :v, :a, :b p a[:k] #=> :v p a[:a] #=> :b p a.fields #=> [:k, :a] p a.values #=> [:v, :b] p a #=> [:v, :b] p a.to_hash #=> {:k => :v, :a => :b} p a.pairs #=> [[:k, :v], [:a, :b]] a[:foo] = :bar p a[:foo] #=> :bar p a.fields #=> [:k, :a, :foo] AND require 'arrayfields' fields = 'name', 'age' a = [ 'zaphod', 42 ] a.fields = fields a['name'] #=> 'zaphod' a[:name ] #=> 'zaphod' a.indices 'name', 'age' #=> [ 'zaphod', 42 ] DESCRIPTION allow keyword access to array instances. arrayfields works by adding only a few methods to arrays, namely #fields= and fields, but the #fields= method is hooked to extend an array on a per object basis. in otherwords __only__ those arrays whose fields are set will have auto-magical keyword access bestowed on them - all other arrays remain unaffected. arrays with keyword access require much less memory when compared to hashes/objects and yet still provide fast lookup and preserve data order. LIST OF OVERRIDDEN METHODS Array#[] Array#slice Array#[]= Array#at Array#delete_at Array#fill Array#values_at Array#indices Array#indexes Array#slice! LIST OF HASH-LIKE METHODS Array#each_with_field Array#each_pair Array#each_key Array#each_value Array#fetch Array#has_key? Array#member? Array#key? Array#has_value? Array#value? Array#keys Array#store Array#values Array#to_hash Array#to_h Array#update Array#replace Array#invert Array#pairs LIST OF ADDED Array METHODS Array#fields= Array#fields LIST OF ADDED Array CLASS METHODS Array.fields/Array.struct SAMPLES <========< sample/a.rb >========> ~ > cat sample/a.rb require 'arrayfields' # # the class Array has only a few added method, one is for setting the fields, # when the fields are set for an array THIS INSTANCE ONLY will be modified to # allow keyword access. other arrays will not be affected! # a = [0,1,2] fields = ['zero', 'one', 'two'] a.fields = fields # ONLY the Array 'a' is affected! # # keyword access is now allowed for many methods # p a['zero'] #=> 0 p a['one'] #=> 1 p a['two'] #=> 2 p a.at('one') #=> 1 p a.values_at('zero', 'two') #=> [0, 2] # # assigmnet is allowed # a['zero'] = 42 p a['zero'] #=> 0 a['zero'] = 0 # # assignment to non-fields results in the element being appended and the field # being added for future use (also appended) # p(a.fields.join(',')) #=> "zero, one, two" p a['three']