I use an array to store fieldvalues. To easily add and access elements I use constants as element identifiers.
Until now I did this by hand like this:
stages = ["lidnummer","aardf","prest","dt_prest","aantal","bedrag","verstrekker","foutcode"]
values = ["it","can","be","anything",0,5.3,nil,88]
LIDNUMMER,AARDF,PREST,DT_PREST,AANTAL,BEDRAG,VERSTREKKER,FOUTCODE = 0,1,2,3,1,5,6,7
p values[AARDF] => "can"
Now I have automated this like:
stages = ["lidnummer","aardf","prest","dt_prest","aantal","bedrag","verstrekker","foutcode"]
values = ["it","can","be","anything",0,5.3,nil,88]
stages.each do |c|
eval "#{c.upcase} = #{stages.index(c)}"
end
p values[AARDF] => "can"
But I suppose there is a better Ruby-way to do this, and perhaps without the eval, are there suggestions?
Thanks all, i learned a lot of this.
Michael was the nearest with his answer but not right (the values array can contain anything, normally it is not the order of the element).
I managed to combine the different techniques here presented and came up with the following.
If i use a Hash it is more error sensitive and i would end using
which is not as smooth