[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Special variable within iterators to hold results?

Wes Gamble

10/2/2006 4:45:00 PM

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?

Thanks,
Wes

--
Posted via http://www.ruby-....

10 Answers

Eero Saynatkari

10/2/2006 4:53:00 PM

0

On 2006.10.03 01:45, 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

Take a look at Enumerable, specifically #inject.

Logan Capaldo

10/2/2006 4:55:00 PM

0

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-....

Rick DeNatale

10/2/2006 8:58:00 PM

0

On 10/2/06, Wes Gamble <weyus@att.net> 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?

filter_columns = displayable_columns.inject([] {|
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Rick DeNatale

10/2/2006 9:08:00 PM

0

On 10/2/06, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 10/2/06, Wes Gamble <weyus@att.net> 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?
>
> filter_columns = displayable_columns.inject([] {|

darn that itchy trigger finger.

displayable_columns.inject([]) {|result, field_array| result <<
[field_array[1], field_array[0]]}

or assuming that field_array contains two element arrays:

displayable_columns.inject([]) {|result, field_array| result <<
[field_array[1], field_array[0]]}


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Rick DeNatale

10/2/2006 9:10:00 PM

0

On 10/2/06, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 10/2/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

Okay, one last try

> or assuming that field_array contains two element arrays:

displayable_columns.inject([]) {|result, field_array| result <<
field_array.reverse}

> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Timothy Goddard

10/3/2006 11:45:00 PM

0

Firstly, you shouldn't capitalize your variables like that. Ruby will
treat any variable with a capital first letter as a constant - it will
behave differently.

A better implementation would be:

filter_columns = displayable_columns.map {|field_array|
[field_array[1], field_array[0]]

or

filter_columns = displayable_columns.map {|field_array|
field_array[0,2].reverse]

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?
>
> Thanks,
> Wes
>
> --
> Posted via http://www.ruby-....

dblack

10/4/2006 12:01:00 AM

0

Caleb Clausen

10/4/2006 4:51:00 AM

0

I like this way:

filter_columns = displayable_columns.map {|(a,b)| [b,a] }

Wes Gamble

10/4/2006 5:40:00 AM

0

Timothy Goddard wrote:
> Firstly, you shouldn't capitalize your variables like that. Ruby will
> treat any variable with a capital first letter as a constant - it will
> behave differently.

Who said it wasn't a constant ;)? It's a set of static lookup data to
drive an options array for a SELECT form element in Rails.

Thanks for looking out for me though.

Wes


--
Posted via http://www.ruby-....

Wes Gamble

10/4/2006 5:50:00 AM

0

Caleb Clausen wrote:
> I like this way:
>
> filter_columns = displayable_columns.map {|(a,b)| [b,a] }

Nice! That wins! :)

Thanks,
Wes

--
Posted via http://www.ruby-....