Loop improvement

Discussion in 'Ruby' started by EdUarDo, Aug 17, 2005.

  1. EdUarDo

    EdUarDo Guest

    Hi all, I have this code:


    class Test
    def testA
    home_squad = home.squad.to_a
    ###########################################
    # Could improve this?
    ###########################################
    for i in 0...11
    home.tactic.set_player(i, home_squad[0])
    end
    end
    end

    class Tactic
    def set_player(counter_id, player_id)
    @counters[counter_id].player_id = player_id
    end
    end

    Having that home.squad is a Hash object, and @counters an Array,
    is there an easy way to do what I'm trying to do?

    In loop for of testA method I want to change the player_id attribute of
    objects contained in @counters array. Could I define an iterator to simplify
    the sentence?
     
    EdUarDo, Aug 17, 2005
    #1
    1. Advertising

  2. On Aug 17, 2005, at 9:41 AM, EdUarDo wrote:

    > Hi all, I have this code:
    >
    >
    > class Test
    > def testA
    > home_squad = home.squad.to_a
    > ###########################################
    > # Could improve this?
    > ###########################################
    > for i in 0...11
    > home.tactic.set_player(i, home_squad[0])
    > end
    > end
    > end
    >
    > class Tactic
    > def set_player(counter_id, player_id)
    > @counters[counter_id].player_id = player_id
    > end
    > end
    >
    > Having that home.squad is a Hash object, and @counters an Array,
    > is there an easy way to do what I'm trying to do?
    >
    > In loop for of testA method I want to change the player_id
    > attribute of
    > objects contained in @counters array. Could I define an iterator to
    > simplify
    > the sentence?


    Here's one thought:

    class Test
    def testA
    home_squad = home.squad.to_a
    home_squad.each_with_index do |squad, i|
    home.tactic.set_player(i, squad.first)
    end
    end
    end

    class Tactic
    def set_player(counter_id, player_id)
    @counters[counter_id].player_id = player_id
    end
    end

    Another idea it to expose the @counters from Tactic:

    class Test
    def testA
    home_squad = home.squad.to_a.map { |e| e.first }
    home.tactic.counters.each { |c| c.player_id =
    home_squad.shift }
    end
    end

    class Tactic
    attr_reader :counters
    end

    Or, you could delegate the iteration:

    class Test
    def testA
    home_squad = home.squad.to_a.map { |e| e.first }
    home.tactic.players { |c| c.player_id = home_squad.shift }
    end
    end

    class Tactic
    def players( &block )
    @counters.each(&block)
    end
    end

    Hope that gives you some new ideas.

    James Edward Gray II
     
    James Edward Gray II, Aug 17, 2005
    #2
    1. Advertising

  3. EdUarDo

    EdUarDo Guest

    James Edward Gray II wrote:
    > Hope that gives you some new ideas.


    All are good ideas. Sometimes I need to cry because of my rigid brain :(,
    well, I think I need more experience with Ruby.
     
    EdUarDo, Aug 17, 2005
    #3
  4. EdUarDo

    EdUarDo Guest

    > home_squad = home.squad.to_a.map { |e| e.first }

    Which is the e's class?
     
    EdUarDo, Aug 17, 2005
    #4
  5. On 17/08/05, EdUarDo <> wrote:
    > > home_squad =3D home.squad.to_a.map { |e| e.first }

    >=20
    > Which is the e's class?
    >=20
    >=20


    You could ask ruby for an answer:

    home_squad =3D home.squad.to_a.map { |e| p e.class; e.first }

    should give you the answer (multiple times even ;)

    regards,

    Brian

    --=20
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Aug 17, 2005
    #5
  6. On Aug 17, 2005, at 10:11 AM, EdUarDo wrote:

    >> home_squad = home.squad.to_a.map { |e| e.first }
    >>

    >
    > Which is the e's class?


    Whatever the class of home_squad[n] is. map() just walks the Array,
    replacing the elements.

    It looked like an Array to me, by the way you were using it.

    James Edward Gray II
     
    James Edward Gray II, Aug 17, 2005
    #6
    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. JV
    Replies:
    4
    Views:
    1,627
  2. =?Utf-8?B?TWFyYyBIb2Vpam1hbnM=?=

    Performance improvement in the 2.0 framework

    =?Utf-8?B?TWFyYyBIb2Vpam1hbnM=?=, Mar 22, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    416
    Alvin Bruney - ASP.NET MVP
    Mar 22, 2006
  3. MaxMustermann

    Improvement on website

    MaxMustermann, Jul 23, 2005, in forum: HTML
    Replies:
    9
    Views:
    375
    MaxMustermann
    Jul 25, 2005
  4. sree
    Replies:
    2
    Views:
    442
    Edward A. Feustel
    Apr 17, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page