# Iterate Array and (pretty)print

**URL:** <https://rubytalk.org/t/iterate-array-and-pretty-print/63580>\
**Category:** ruby-talk\
**Created:** [13 September 2011 14:33 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580 "2011-09-13T14:33:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwight\_schrute1](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dwight\_schrute1](https://rubytalk.org/u/dwight_schrute1)\
**Post date:** [13 September 2011 14:33 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/1 "2011-09-13T14:33:58Z")

</div>

Hi,

I am trying to iterate through an array, convert the output to a string  
and puts that string.

CODE:

@hosts = ["test1", "test2", "test3", "test4"  
puts @hosts.each { |x| print x + "\n" }.to\_s

CURRENT OUTPUT:

["test1", "test2", "test3", "test4"]

DESIRED OUTPUT:

However, I want this output to go into an email message, so I'm want to  
lose the Square brackets and quotes, so it looks like this (and is a  
string):

test1  
test2  
test3

Any ideas?

Thanks.

Dwight

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Chris\_White](https://avatars.discourse-cdn.com/v4/letter/c/e56c9b/32.png) [@Chris\_White](https://rubytalk.org/u/Chris_White)\
**Post date:** [13 September 2011 14:37 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/2 "2011-09-13T14:37:58Z")

</div>

> I am trying to iterate through an array, convert the output to a string  
> and puts that string.
> 
> CODE:
> 
> @hosts = ["test1", "test2", "test3", "test4"  
> puts @hosts.each { |x| print x + "\n" }.to\_s
> 
> CURRENT OUTPUT:
> 
> ["test1", "test2", "test3", "test4"]
> 
> DESIRED OUTPUT:
> 
> However, I want this output to go into an email message, so I'm want to  
> lose the Square brackets and quotes, so it looks like this (and is a  
> string):
> 
> test1  
> test2  
> test3
> 
> Any ideas?

Hi there,

In this case I think a simple join would be appropriate:

irb(main):001:0\> @hosts = ["test1", "test2", "test3", "test4"]  
=\> ["test1", "test2", "test3", "test4"]  
irb(main):002:0\> puts @hosts.join("\n")  
test1  
test2  
test3  
test4

Regards,  
Chris White  
[http://www.twitter.com/cwgem](http://www.twitter.com/cwgem)

---

<div class="post-metadata">

**Author:** ![Larry\_Lv](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Larry\_Lv](https://rubytalk.org/u/Larry_Lv)\
**Post date:** [13 September 2011 14:40 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/3 "2011-09-13T14:40:32Z")

</div>

hi,

u have output @hosts' elements in the block? why would you add a puts in  
front of it?

u can do it just with Array.join

\*puts hosts.join("\n")\*

> **···**
>
> On Tue, Sep 13, 2011 at 10:33 PM, dwight schrute \<spambocks@yahoo.ca\> wrote:
> 
> > Hi,
> > 
> > I am trying to iterate through an array, convert the output to a string  
> > and puts that string.
> > 
> > CODE:
> > 
> > @hosts = ["test1", "test2", "test3", "test4"  
> > puts @hosts.each { |x| print x + "\n" }.to\_s
> > 
> > CURRENT OUTPUT:
> > 
> > ["test1", "test2", "test3", "test4"]
> > 
> > DESIRED OUTPUT:
> > 
> > However, I want this output to go into an email message, so I'm want to  
> > lose the Square brackets and quotes, so it looks like this (and is a  
> > string):
> > 
> > test1  
> > test2  
> > test3
> > 
> > Any ideas?
> > 
> > Thanks.
> > 
> > Dwight
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Best Regards,
> 
> Larry Lv  
> @ Baidu NLP

---

<div class="post-metadata">

**Author:** ![dwight\_schrute1](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dwight\_schrute1](https://rubytalk.org/u/dwight_schrute1)\
**Post date:** [13 September 2011 15:04 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/4 "2011-09-13T15:04:28Z")

</div>

Except, I need to put the output into a string. When I use the to\_s  
method, I end up with square brackets and quotes:

irb(main):001:0\> @hosts = ["test1", "test2", "test3", "test4"]  
=\> ["test1", "test2", "test3", "test4"]  
irb(main):002:0\> puts @hosts  
test1  
test2  
test3  
test4  
=\> nil  
irb(main):003:0\> puts @hosts.to\_s  
["test1", "test2", "test3", "test4"]  
=\> nil

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![dwight\_schrute1](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dwight\_schrute1](https://rubytalk.org/u/dwight_schrute1)\
**Post date:** [13 September 2011 15:11 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/5 "2011-09-13T15:11:04Z")

</div>

Thank you Harry! That is exactly what I needed 🙂

Dwight

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@Adam\_Prescott](https://rubytalk.org/u/Adam_Prescott)\
**Post date:** [13 September 2011 21:12 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/6 "2011-09-13T21:12:12Z")

</div>

Just for completeness since nobody has quite covered it yet,  
@hosts.each { ... } returns the receiver, which in this case is  
@hosts, and then you call .to\_s on the result. So, closer to the  
argument actually passed to puts, you essentially have @hosts.to\_s  
which is "[\"test1\", \"test2\", \"test3\", \"test4\"]", which, when  
puts'd, is ["test1", "test2", "test3", "test4"]. However, in your  
block you also print, so the real, full output should have been:

test1  
test2  
test3  
test4  
["test1", "test2", "test3", "test4"]

> **···**
>
> On Tue, Sep 13, 2011 at 3:33 PM, dwight schrute \<spambocks@yahoo.ca\> wrote:
> 
> > puts @hosts.each { |x| print x + "\n" }.to\_s

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 September 2011 14:51 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/7 "2011-09-13T14:51:25Z")

</div>

You don't even need the join.

irb(main):001:0\> @hosts = ["test1", "test2", "test3", "test4"]  
=\> ["test1", "test2", "test3", "test4"]  
irb(main):002:0\> puts @hosts  
test1  
test2  
test3  
test4  
=\> nil  
irb(main):003:0\>

Cheers

robert

> **···**
>
> On Tue, Sep 13, 2011 at 4:40 PM, Larry Lv \<larrylv1990@gmail.com\> wrote:
> 
> > hi,
> > 
> > u have output @hosts' elements in the block? why would you add a puts in  
> > front of it?
> > 
> > u can do it just with Array.join
> > 
> > \*puts hosts.join("\n")\*
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [13 September 2011 15:06 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/8 "2011-09-13T15:06:48Z")

</div>

> You don't even need the join.
> 
> irb(main):001:0\> @hosts = ["test1", "test2", "test3", "test4"]  
> =\> ["test1", "test2", "test3", "test4"]  
> irb(main):002:0\> puts @hosts  
> test1  
> test2  
> test3  
> test4  
> =\> nil  
> irb(main):003:0\>
> 
> Cheers
> 
> robert

But, he wants it to be a string.

print @hosts.map{|x| x + "\n"}.join

Harry

---

<div class="post-metadata">

**Author:** ![Peter\_Vandenabeele1](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@Peter\_Vandenabeele1](https://rubytalk.org/u/Peter_Vandenabeele1)\
**Post date:** [13 September 2011 15:17 UTC](https://rubytalk.org/t/iterate-array-and-pretty-print/63580/9 "2011-09-13T15:17:16Z")

</div>

> \> hi,  
> \>  
> \> u have output @hosts' elements in the block? why would you add a puts in  
> \> front of it?  
> \>  
> \> u can do it just with Array.join  
> \>  
> \> \*puts hosts.join("\n")m\*
> 
> You don't even need the join.
> 
> irb(main):001:0\> @hosts = ["test1", "test2", "test3", "test4"]  
> =\> ["test1", "test2", "test3", "test4"]  
> irb(main):002:0\> puts @hosts  
> test1  
> test2  
> test3  
> test4  
> =\> nil  
> irb(main):003:0\>

I think this is just an artefact of "puts" and not relevant for the initial  
problem.

If the original posters wants to have it in an e-mail, he will probably will  
want to interpolate in a template. Using simply the @hosts as is suggested  
above will then yield this:

$ ruby \<\<EOF  
@hosts = ["test1", "test2", "test3", "test4"]  
puts "#{@hosts}"

> EOF

test1test2test3test4

I think the OP probably needed to use 'map' instead of 'each'.

pev@pev-desktop:~/a$ ruby \<\<EOF  
@hosts = ["test1", "test2", "test3", "test4"]  
hosts\_string = @hosts.join("\n")

> puts hosts\_string  
> EOF

test1  
test2  
test3  
test4

HTH,

Peter

> **···**
>
> On Tue, Sep 13, 2011 at 4:51 PM, Robert Klemme \<shortcutter@googlemail.com\>wrote:
> 
> > On Tue, Sep 13, 2011 at 4:40 PM, Larry Lv \<larrylv1990@gmail.com\> wrote:
> 
> > Cheers
> > 
> > robert
> > 
> > --  
> > remember.guy do |as, often| as.you\_can - without end  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> 
> --  
> Peter Vandenabeele  
> gsm: +32-478-27.40.69  
> e-mail: peter@vandenabeele.com  
> [http://twitter.com/peter\_v](http://twitter.com/peter_v)
