Block iteration issues

Discussion in 'Ruby' started by Gromobij Swietlisty, Oct 19, 2007.

  1. Hello,

    I'm struggling with this stuff and can't find an answer so please
    advise.
    What I'm trying to achieve

    @collection has 7 elements
    I want to:
    - cycle through all those elements
    - assign element["smth"] through element["smth3"] to @ data.set1_smth
    through @data.set1_smth3
    - the last iteration would be assigning element["smth"] to
    @data.set7_smth etc

    I just don't seem to grasp how to change @data.set1_smth into
    @data.set2_smth etc. when iterating the block.

    i = 1
    @collection.elements.each do |item|
    @data.set1_smth = item.elements["smth"] # obviously
    @data.set1_smth should be somehow different
    @data.set1_smth2 = item.elements["smth2"]
    @data.set1_smth3 = item.elements["smth3"]
    i += 1
    end

    Can anybody help?
    I tried @data.send("set" + i + "_smth") but that didn't seem to work.
    Thanks in advance.

    Radek
    --
    Posted via http://www.ruby-forum.com/.
     
    Gromobij Swietlisty, Oct 19, 2007
    #1
    1. Advertising

  2. Gromobij Swietlisty

    7stud -- Guest

    Gromobij Swietlisty wrote:
    > Hello,
    >
    > I'm struggling with this stuff and can't find an answer so please
    > advise.
    > What I'm trying to achieve
    >
    > @collection has 7 elements
    > I want to:
    > - cycle through all those elements
    > - assign element["smth"] through element["smth3"] to @ data.set1_smth
    > through @data.set1_smth3
    > - the last iteration would be assigning element["smth"] to
    > @data.set7_smth etc
    >
    > I just don't seem to grasp how to change @data.set1_smth into
    > @data.set2_smth etc. when iterating the block.
    >
    > i = 1
    > @collection.elements.each do |item|
    > @data.set1_smth = item.elements["smth"] # obviously
    > @data.set1_smth should be somehow different
    > @data.set1_smth2 = item.elements["smth2"]
    > @data.set1_smth3 = item.elements["smth3"]
    > i += 1
    > end
    >
    > Can anybody help?
    > I tried @data.send("set" + i + "_smth") but that didn't seem to work.
    > Thanks in advance.
    >
    > Radek


    - assign element["smth"] through element["smth3"] to @ data.set1_smth
    through @data.set1_smth3


    If you are naming variables like this:

    val1 = 5
    val2 = 7
    val3 = -3

    that tells you that you should be using an array instead:

    arr = []
    arr[0] = 5
    arr[1] = 7
    arr[2] = -3


    >
    > @collection.elements
    >


    ------------------------------------------------------- Vector::elements
    Vector::elements(array, copy = true)
    ------------------------------------------------------------------------
    Creates a vector from an Array. The optional second argument
    specifies whether the array itself or a copy is used internally.


    Why are you creating a Vector in order to step through the items of your
    array?

    >
    > Can anybody help?
    >


    collection = [1, 2, 3, 4, 5, 6, 7]
    data = []

    collection.each do |item|
    data << item
    end

    p data


    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 19, 2007
    #2
    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. Showjumper
    Replies:
    1
    Views:
    724
    Showjumper
    Mar 19, 2005
  2. Noozer

    Block DIV within a block DIV?

    Noozer, Jan 6, 2005, in forum: HTML
    Replies:
    3
    Views:
    11,442
    Mitja
    Jan 6, 2005
  3. Andy
    Replies:
    0
    Views:
    560
  4. morrell
    Replies:
    1
    Views:
    1,006
    roy axenov
    Oct 10, 2006
  5. Rudi
    Replies:
    5
    Views:
    5,321
Loading...

Share This Page