Show elements of array separated

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?

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/

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

···

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?

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

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

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/

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

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

  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

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

    robert

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

···

--
Michael Morin
Guide to Ruby

Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company

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

    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 :slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

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

    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 :slight_smile:

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

Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company