From: Mat Schaffer Date: 2006-06-20T05:10:21+09:00 Subject: Re: associative array naming indirection On Jun 19, 2006, at 3:51 PM, dan donaldson wrote: > I have an array of names in string form as an instance var. The class > the array is in has a method that takes an array of string values. > > I want to construct an associative array such that the names in the > first array are the keys to the values in the received array. > > this throws an error: > -------------- > def initialize > @labels = ['prov', 'area', 'zone', district', 'postalcode', > 'city_prov', 'addr'] > @pageData = {} > end > > def adddata(data) > max = data.length > max -= 1 > 0.upto(max) {|i| @pageData[@labels[i]] => data[i] } > end > > -------------- > > obviously, some giant conceptual leap needs to be made, or its a > miniscule typo. (methinks the former) > > Anyone got advice? It's hard to know without the error, but adddata() doesn't look great to me. Try something like this: def add_data(data) data.each_index do |index| @pageData[@labels[i]] = data[i] end end -Mat