# Block iteration issues

**URL:** <https://www.ruby-forum.com/t/block-iteration-issues/117732>\
**Category:** Ruby\
**Created:** [October 19, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117732 "2007-10-19T19:33:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sabon](https://www.ruby-forum.com/u/sabon)\
**Post date:** [October 19, 2007, 7:33pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117732/1 "2007-10-19T19:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sabon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sabon](https://www.ruby-forum.com/u/sabon)\
**Post date:** [October 19, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117732/2 "2007-10-19T20:54:25Z")

</div>

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
