On Tue, Oct 03, 2006 at 01:45:19AM +0900, Wes Gamble wrote:
> I have this:
> 
> FILTER_COLUMNS = Array.new
> DISPLAYABLE_COLUMNS.each do |field_array|
>   FILTER_COLUMNS << [ field_array[1], field_array[0] ]
> end
> 
> Is there any way to write this so that it could look something like:
> 
> FILTER_COLUMNS = DISPLAYABLE_COLUMNS.each do |field_array|
>   ??? << [ field_array[1], field_array[0] ]
> end
> 
> so that I don't have to bother initializing FILTER_COLUMNS - is there 
> some special variable that holds the intermediate result of the iterator 
> body?
> 
> Or perhaps an appropriate call to collect?
> 
That would work:
FILTER_COLUMNS = DISPLAYABLE_COLUMNS.collect { |field_array| [field_array[1],
field_array[0] }

Note you can also do something like:

filter_cols = disp_cols.map { |a, b, *_| [b, a] }

( map and collect are aliases of each other. )
> Thanks,
> Wes
> 
> -- 
> Posted via http://www.ruby-forum.com/.