# Ruby Array -\> String Conversion Issue

**URL:** <https://www.ruby-forum.com/t/ruby-array-string-conversion-issue/77371>\
**Category:** Ruby\
**Created:** [March 6, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/ruby-array-string-conversion-issue/77371 "2006-03-06T17:56:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 6, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/ruby-array-string-conversion-issue/77371/1 "2006-03-06T17:56:58Z")

</div>

(Newbie Alert!)

I have a CGI script that takes an address and tries to put it as hidden  
form data on a new page. “post” is a hash of POST data.

[“addr1”, “addr2”, “city”, “state”, “zip”, “country”].each do |part|  
puts “\<input type=“hidden” name=”" + part.to\_s + “” value="" +  
post[part.to\_s] + “” /\>"  
end

This code generates the complaint that “part” is an array, and thus  
cannot be converted to a string. How do I iterate over each item in that  
array of address data?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 6, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/ruby-array-string-conversion-issue/77371/2 "2006-03-06T18:28:04Z")

</div>

On Mar 6, 2006, at 5:57 PM, Nathan O. wrote:

> This code generates the complaint that “part” is an array, and thus  
> cannot be converted to a string. How do I iterate over each item in  
> that  
> array of address data?

I see no problem with that code, except I would write it as:

%w{addr1 addr2 city state zip country}.each do |part|  
puts %{}  
end

You don’t have another local variable named part, do you?

When you convert an array to a string, all the elements just get  
mashed together: %w{foo bar baz}.to\_s -\> “foobarbaz”

– Daniel

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [March 7, 2006, 1:44am UTC](https://www.ruby-forum.com/t/ruby-array-string-conversion-issue/77371/3 "2006-03-07T01:44:55Z")

</div>

Ah, my mistake. I assumed it was talking about my “part” variable when  
it was talking about my reference to post[part.to\_s], which returns an  
array. I’m not sure it’s the tidiest fix, but I changed this to  
post[part.to\_s].to\_s, and it works. Well, that part works, now I have  
other issues 🙂

> %w{addr1 addr2 city state zip country}.each do |part|  
> puts %{}  
> end

This looks like one of those things I’m going to have to look up! Thanks  
for the pointer!
