Special variable within iterators to hold results?

Discussion in 'Ruby' started by Wes Gamble, Oct 2, 2006.

  1. Wes Gamble

    Wes Gamble Guest

    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-forum.com/.
     
    Wes Gamble, Oct 2, 2006
    #1
    1. Advertising

  2. --QKpLca3blcvhMJ0W
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On 2006.10.03 01:45, Wes Gamble wrote:
    > I have this:
    >=20
    > FILTER_COLUMNS =3D Array.new
    > DISPLAYABLE_COLUMNS.each do |field_array|
    > FILTER_COLUMNS << [ field_array[1], field_array[0] ]
    > end
    >=20
    > Is there any way to write this so that it could look something like:
    >=20
    > FILTER_COLUMNS =3D DISPLAYABLE_COLUMNS.each do |field_array|
    > ??? << [ field_array[1], field_array[0] ]
    > end


    Take a look at Enumerable, specifically #inject.


    --QKpLca3blcvhMJ0W
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (FreeBSD)

    iD8DBQFFIUOI7Nh7RM4TrhIRAmHeAKDxbGP9ataNwmYlVnZO7NHMvuNvLACfTK63
    iP6+bmciThSyRV4yc7gkRpo=
    =J6qC
    -----END PGP SIGNATURE-----

    --QKpLca3blcvhMJ0W--
     
    Eero Saynatkari, Oct 2, 2006
    #2
    1. Advertising

  3. 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/.
     
    Logan Capaldo, Oct 2, 2006
    #3
  4. On 10/2/06, 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?


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

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/

    IPMS/USA Region 12 Coordinator
    http://ipmsr12.denhaven2.com/

    Visit the Project Mercury Wiki Site
    http://www.mercuryspacecraft.com/
     
    Rick DeNatale, Oct 2, 2006
    #4
  5. On 10/2/06, Rick DeNatale <> wrote:
    > On 10/2/06, 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?

    >
    > 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.denhaven2.com/
     
    Rick DeNatale, Oct 2, 2006
    #5
  6. On 10/2/06, Rick DeNatale <> wrote:
    > On 10/2/06, Rick DeNatale <> 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.denhaven2.com/
    >



    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/

    IPMS/USA Region 12 Coordinator
    http://ipmsr12.denhaven2.com/

    Visit the Project Mercury Wiki Site
    http://www.mercuryspacecraft.com/
     
    Rick DeNatale, Oct 2, 2006
    #6
  7. 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-forum.com/.
     
    Timothy Goddard, Oct 4, 2006
    #7
  8. Wes Gamble

    Guest

    Hi --

    On Wed, 4 Oct 2006, Timothy Goddard wrote:
    >
    > 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?
    >>

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


    You could also do:

    filter_columns = displayable_columns.map {|field_array|
    field_array.values_at(1,0) }


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Oct 4, 2006
    #8
  9. I like this way:

    filter_columns = displayable_columns.map {|(a,b)| [b,a] }
     
    Caleb Clausen, Oct 4, 2006
    #9
  10. Wes Gamble

    Wes Gamble Guest

    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-forum.com/.
     
    Wes Gamble, Oct 4, 2006
    #10
  11. Wes Gamble

    Wes Gamble Guest

    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-forum.com/.
     
    Wes Gamble, Oct 4, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    488
    Kai-Uwe Bux
    May 8, 2005
  2. sai krishna
    Replies:
    1
    Views:
    307
    Patricia Shanahan
    Dec 24, 2007
  3. , India
    Replies:
    10
    Views:
    1,081
    James Kanze
    Aug 8, 2009
  4. dale zhang
    Replies:
    8
    Views:
    426
    Tintin
    Nov 30, 2004
  5. John Reye
    Replies:
    14
    Views:
    786
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page