# Iterating variables inside a block

**URL:** <https://www.ruby-forum.com/t/iterating-variables-inside-a-block/112767>\
**Category:** Rails\
**Created:** [August 23, 2007, 1:18am UTC](https://www.ruby-forum.com/t/iterating-variables-inside-a-block/112767 "2007-08-23T01:18:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![glimjaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3be4f8/32.png) [@glimjaur](https://www.ruby-forum.com/u/glimjaur)\
**Post date:** [August 23, 2007, 1:18am UTC](https://www.ruby-forum.com/t/iterating-variables-inside-a-block/112767/1 "2007-08-23T01:18:29Z")

</div>

I am currently using the ActiveMerchant plugin to create a Paypal  
order, but that is not where my problem is.

I am trying to iterate over a variable inside a block, this is what i  
got;

# Creating item\_x in order to display each items in the Paypal

checkout  
i = 1  
for order\_item in @order.order\_items do  
service.item\_name\_i = order\_item.product.name  
#service.item\_quantity\_i = order\_item.quantity  
service.amount\_i = order\_item.price  
i+1  
end

This is the effect i want to have;

service.item\_name\_1 = Name of item 1 in the block  
service.item\_quantity\_1 = Quantity of item 1  
service.amount\_1 = Price of item 1

service.item\_name\_2 = Name of item 2 in the block  
…

I hope i am making myself understandable, i would really appreciate  
some help. I am sure this could easily be done, but i cant figure it  
out…

---

<div class="post-metadata">

**Author:** ![glimjaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3be4f8/32.png) [@glimjaur](https://www.ruby-forum.com/u/glimjaur)\
**Post date:** [August 23, 2007, 1:43am UTC](https://www.ruby-forum.com/t/iterating-variables-inside-a-block/112767/2 "2007-08-23T01:43:30Z")

</div>

> service.item\_name\_i = order\_item.product.name
> 
> service.item\_name\_2 = Name of item 2 in the block  
> …

Where does the initial “service” get defined? Unless you’ve got some  
magic going on there that lets you dynamically add those instance  
variables (or methods if that’s what they are) the above isn’t going to  
work even if you were getting ‘i’ to be interpreted correctly.

What’s your end goal?
