# Block iteration issues

**URL:** <https://www.ruby-forum.com/t/block-iteration-issues/117688>\
**Category:** Rails\
**Created:** [October 19, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117688 "2007-10-19T12:20:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117688/1 "2007-10-19T12:20:31Z")

</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\_smthetc

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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/block-iteration-issues/117688/2 "2007-10-19T12:27:32Z")

</div>

On 19 Oct 2007, at 11:19, [s] wrote:

> Can anybody help?  
> I tried @data.send(“set” + i + “\_smth”) but that didn’t seem to work.  
> Thanks in advance.

This is a ruby rather than a rails question, but you need to send the  
method name “set1\_smth=”, and pass the value to be set as the next  
argument

Fred
