Class | FieldedArray |
In: |
lib/arrayfields.rb
|
Parent: | Object |
proxy class that allows an array to be wrapped in a way that still allows # keyword access. also facilitate usage of ArrayFields with arraylike objects. thnx to Sean O’Dell for the suggestion.
sample usage
fa = FieldedArray.new %w(zero one two), [0,1,2] p fa[‘zero’] #=> 0
# File lib/arrayfields.rb, line 286 def [](*pairs) #{{{ pairs.flatten! raise ArgumentError, "argument must be key/val paris" unless (pairs.size % 2 == 0 and pairs.size >= 2) fields, elements = [], [] #pairs.each do |f,e| while((f = pairs.shift) and (e = pairs.shift)) raise ArgumentError, "field must be String or Symbol" unless (String === f or Symbol === f) fields << f and elements << e end new fields, elements #}}} end
# File lib/arrayfields.rb, line 303 def initialize fields, array #{{{ @a = array self.fields = fields #}}} end