# Show elements of array separated

**URL:** <https://rubytalk.org/t/show-elements-of-array-separated/48745>\
**Category:** ruby-talk\
**Created:** [30 August 2008 08:05 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745 "2008-08-30T08:05:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![GinTon](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GinTon](https://rubytalk.org/u/GinTon)\
**Post date:** [30 August 2008 08:05 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/1 "2008-08-30T08:05:30Z")

</div>

Why when is showed an array into a variable , it is showed with all  
characters followed?

> **···**
>
> ---------------  
> foo = ['a', 'b', 'c']  
> puts "the content is: #{foo}" # =\> the content is: abc  
> ---------------
> 
> I'm supposed that is because at the first it converts the array into a  
> string. But is there any way of show the elements of array separated?

---

<div class="post-metadata">

**Author:** ![Dick\_Davies1](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dick\_Davies1](https://rubytalk.org/u/Dick_Davies1)\
**Post date:** [30 August 2008 09:17 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/2 "2008-08-30T09:17:11Z")

</div>

> Why when is showed an array into a variable , it is showed with all  
> characters followed?
> 
> ---------------  
> foo = ['a', 'b', 'c']  
> puts "the content is: #{foo}" # =\> the content is: abc

puts "#{foo.join(',')}"

> **···**
>
> On Sat, Aug 30, 2008 at 9:05 AM, Kless \<jonas.esp@googlemail.com\> wrote:
> 
> > ---------------
> > 
> > I'm supposed that is because at the first it converts the array into a  
> > string. But is there any way of show the elements of array separated?
> 
> --  
> Rasputnik :: Jack of All Trades - Master of Nuns  
> [http://number9.hellooperator.net/](http://number9.hellooperator.net/)

---

<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:** [30 August 2008 11:30 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/3 "2008-08-30T11:30:02Z")

</div>

irb(main):001:0\> foo = %w{a b c}  
=\> ["a", "b", "c"]  
irb(main):002:0\> foo.to\_s  
=\> "abc"  
irb(main):003:0\> foo.join ", "  
=\> "a, b, c"  
irb(main):004:0\>

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 30.08.2008 10:09, Kless wrote:
> 
> > Why when is showed an array into a variable , it is showed with all  
> > characters followed?
> > 
> > ---------------  
> > foo = ['a', 'b', 'c']  
> > puts "the content is: #{foo}" # =\> the content is: abc  
> > ---------------
> > 
> > I'm supposed that is because at the first it converts the array into a  
> > string. But is there any way of show the elements of array separated?

---

<div class="post-metadata">

**Author:** ![TPReal](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@TPReal](https://rubytalk.org/u/TPReal)\
**Post date:** [30 August 2008 11:35 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/4 "2008-08-30T11:35:58Z")

</div>

Kless wrote:

> Why when is showed an array into a variable , it is showed with all  
> characters followed?
> 
> ---------------  
> foo = ['a', 'b', 'c']  
> puts "the content is: #{foo}" # =\> the content is: abc  
> ---------------
> 
> I'm supposed that is because at the first it converts the array into a  
> string. But is there any way of show the elements of array separated?

puts foo.inspect

It will look exactly like in irb, because irb uses inspect.

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

---

<div class="post-metadata">

**Author:** ![Dan6](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dan6](https://rubytalk.org/u/Dan6)\
**Post date:** [30 August 2008 16:07 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/5 "2008-08-30T16:07:17Z")

</div>

foo = ['a', 'b', 'c']  
puts "the content is: #{foo.join(', ')}."

---

<div class="post-metadata">

**Author:** ![Michael\_Fellinger1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Michael\_Fellinger1](https://rubytalk.org/u/Michael_Fellinger1)\
**Post date:** [30 August 2008 09:47 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/6 "2008-08-30T09:47:32Z")

</div>

puts arr.join(', ')

> **···**
>
> On 8/30/08, Dick Davies \<rasputnik@hellooperator.net\> wrote:
> 
> > On Sat, Aug 30, 2008 at 9:05 AM, Kless \<jonas.esp@googlemail.com\> wrote:
> > 
> > > Why when is showed an array into a variable , it is showed with all  
> > > characters followed?
> > > 
> > > ---------------  
> > > foo = ['a', 'b', 'c']  
> > > puts "the content is: #{foo}" # =\> the content is: abc
> > 
> > puts "#{foo.join(',')}"
> > 
> > > ---------------
> > > 
> > > I'm supposed that is because at the first it converts the array into a  
> > > string. But is there any way of show the elements of array separated?
> > 
> > --  
> > Rasputnik :: Jack of All Trades - Master of Nuns  
> > [http://number9.hellooperator.net/](http://number9.hellooperator.net/)

---

<div class="post-metadata">

**Author:** ![GinTon](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GinTon](https://rubytalk.org/u/GinTon)\
**Post date:** [30 August 2008 11:45 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/7 "2008-08-30T11:45:01Z")

</div>

Thanks! I was too complicated:

irb(main):001:0\> foo = ['a', 'b', 'c']  
=\> ["a", "b", "c"]  
irb(main):002:0\> s = ''  
=\> ""  
irb(main):003:0\> foo.each {|x| s += x.to\_s + ', '}  
=\> ["a", "b", "c"]  
irb(main):004:0\> puts s[0..-3]  
a, b, c  
=\> nil

> **···**
>
> On Aug 30, 12:33 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > On 30.08.2008 10:09, Kless wrote:
> > 
> > \> Why when is showed an array into a variable , it is showed with all  
> > \> characters followed?
> > 
> > \> ---------------  
> > \> foo = ['a', 'b', 'c']  
> > \> puts "the content is: #{foo}" # =\> the content is: abc  
> > \> ---------------
> > 
> > \> I'm supposed that is because at the first it converts the array into a  
> > \> string. But is there any way of show the elements of array separated?
> > 
> > irb(main):001:0\> foo = %w{a b c}  
> > =\> ["a", "b", "c"]  
> > irb(main):002:0\> foo.to\_s  
> > =\> "abc"  
> > irb(main):003:0\> foo.join ", "  
> > =\> "a, b, c"  
> > irb(main):004:0\>
> > 
> > Kind regards
> > 
> > ```
> > robert
> > 
> > ```

---

<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:** [30 August 2008 12:05 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/8 "2008-08-30T12:05:06Z")

</div>

If you want to do it manually, I'd rather do something like this:

irb(main):004:0\> s = ""  
=\> ""  
irb(main):005:0\> foo.each\_with\_index {|x,i| s \<\< ", " unless i == 0; s \<\< i.to\_s}  
=\> ["a", "b", "c"]  
irb(main):006:0\> s  
=\> "0, 1, 2"  
irb(main):007:0\>

Or even

irb(main):010:0\> foo.inject {|a,b| "#{a}, #{b}"}  
=\> "a, b, c"  
irb(main):011:0\> ["a"].inject {|a,b| "#{a}, #{b}"}  
=\> "a"  
irb(main):012:0\> .inject {|a,b| "#{a}, #{b}"}  
=\> nil  
irb(main):013:0\>

But note the empty Array case.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 30.08.2008 13:45, Kless wrote:
> 
> > Thanks! I was too complicated:
> > 
> > irb(main):001:0\> foo = ['a', 'b', 'c']  
> > =\> ["a", "b", "c"]  
> > irb(main):002:0\> s = ''  
> > =\> ""  
> > irb(main):003:0\> foo.each {|x| s += x.to\_s + ', '}  
> > =\> ["a", "b", "c"]  
> > irb(main):004:0\> puts s[0..-3]  
> > a, b, c  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![Michael\_Morin](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Michael\_Morin](https://rubytalk.org/u/Michael_Morin)\
**Post date:** [30 August 2008 14:20 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/9 "2008-08-30T14:20:16Z")

</div>

Robert Klemme wrote:

> irb(main):010:0\> foo.inject {|a,b| "#{a}, #{b}"}  
> =\> "a, b, c"  
> irb(main):011:0\> ["a"].inject {|a,b| "#{a}, #{b}"}  
> =\> "a"  
> irb(main):012:0\> .inject {|a,b| "#{a}, #{b}"}  
> =\> nil  
> irb(main):013:0\>
> 
> But note the empty Array case.
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

irb(main):006:0\> .inject{|a,b| "#{a}, #{b}" } or ""  
=\> ""

> **···**
>
> --  
> Michael Morin  
> Guide to Ruby
> 
> > **[Ruby Programming](https://www.thoughtco.com/ruby-programming-4133469)**
> >
> > Learn the Ruby programming language with simple, step-by-step tutorials.
> 
> Become an About.com Guide: beaguide.about.com  
> About.com is part of the New York Times Company

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [30 August 2008 18:21 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/10 "2008-08-30T18:21:39Z")

</div>

Hi --

> **···**
>
> On Sat, 30 Aug 2008, Michael Morin wrote:
> 
> > Robert Klemme wrote:
> > 
> > > irb(main):010:0\> foo.inject {|a,b| "#{a}, #{b}"}  
> > > =\> "a, b, c"  
> > > irb(main):011:0\> ["a"].inject {|a,b| "#{a}, #{b}"}  
> > > =\> "a"  
> > > irb(main):012:0\> .inject {|a,b| "#{a}, #{b}"}  
> > > =\> nil  
> > > irb(main):013:0\>
> > > 
> > > But note the empty Array case.
> > > 
> > > Kind regards
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;robert
> > 
> > irb(main):006:0\> .inject{|a,b| "#{a}, #{b}" } or ""  
> > =\> ""
> 
> Or:
> 
> &nbsp;&nbsp;.inject {...}.to\_s
> 
> In any case I think this is a job for #join 🙂
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Michael\_Morin](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Michael\_Morin](https://rubytalk.org/u/Michael_Morin)\
**Post date:** [30 August 2008 19:57 UTC](https://rubytalk.org/t/show-elements-of-array-separated/48745/11 "2008-08-30T19:57:46Z")

</div>

David A. Black wrote:

> Hi --
> 
> > Robert Klemme wrote:
> > 
> > > irb(main):010:0\> foo.inject {|a,b| "#{a}, #{b}"}  
> > > =\> "a, b, c"  
> > > irb(main):011:0\> ["a"].inject {|a,b| "#{a}, #{b}"}  
> > > =\> "a"  
> > > irb(main):012:0\> .inject {|a,b| "#{a}, #{b}"}  
> > > =\> nil  
> > > irb(main):013:0\>
> > > 
> > > But note the empty Array case.
> > > 
> > > Kind regards
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;robert
> > 
> > irb(main):006:0\> .inject{|a,b| "#{a}, #{b}" } or ""  
> > =\> ""
> 
> Or:
> 
> .inject {...}.to\_s
> 
> In any case I think this is a job for #join 🙂
> 
> David

Using inject you could do something to each element (capitalize, for example) at the same time. Though, you could just use arr.map.join if you wanted to do that.

> **···**
>
> > On Sat, 30 Aug 2008, Michael Morin wrote:
> 
> --  
> Michael Morin  
> Guide to Ruby
> 
> > **[Ruby Programming](https://www.thoughtco.com/ruby-programming-4133469)**
> >
> > Learn the Ruby programming language with simple, step-by-step tutorials.
> 
> Become an About.com Guide: beaguide.about.com  
> About.com is part of the New York Times Company
