From: Robert Klemme Date: 2008-09-12T15:49:22+09:00 Subject: Re: pass by reference in each loop 2008/9/12 Xiong Chiamiov : > Robert Klemme wrote: >> On 11.09.2008 23:12, Xiong Chiamiov wrote: >> >>> Robert's solution would allow me to only write the function name once, >>> but the variables I'd still have to type twice, and I really like DRY. >>> >>> My first thought was to store a list of all those variables in an array >>> and loop through them, as it's then very easy to add an additional >>> variable. Perhaps I'm approaching this the complete wrong way, though? >> >> That's quite near to what I suggested: just drop all the @a, @b etc. and >> stuff everything in an array - or have multiple arrays, one per class of >> data (i.e. data which undergoes wml and data which does not). Anyway, >> chances are that you'll be able to find an elegant solution without >> needing to change the language. > > I don't think I understood what you were suggesting the first time. If > I understand what you're suggesting, it is to have the values themselves > in the array, rather than variables pointing *to* those values, yes? What exactly do you mean by "variable pointing to those values"? This wording does not really seem to make sense in Ruby. The value (object) does not care how many references are around pointing to it. With @x = Object.new @y = Object.new @a = [@x, @y] and @a = [Object.new, Object.new] there is no difference with regard to array contents. I suggest to use the latter and drop the former *if* you have to treat them uniformly more often than not. > I need to know specifically which values are which, however, as they get > passed back into different parts of the output, so I'm thinking I'd have > to use a hash, something like > > hash = { > 'a' => 'this is actually input', > 'b' => 'not hardcoded', > ... > } > > so that I could assign them appropriately afterwards: > > hash.each {|key, value| key = value} > > although, looking at that now, I don't think that it would work. It won't. > But as far as hash mapping (if that's what I have to do), I suppose I > could use one of the methods in this thread > (http://www.nabble.com/Iterating-through-a-hash-td19074540.html), right? Yep. Problem is, you present just a very tiny piece of your application so it is extremely hard for use to suggest an appropriate solution. Cheers robert -- use.inject do |as, often| as.you_can - without end