Idiomatic Printing an array with commas

Hi,

I want to print an array (of strings, mostly) with commas separating the
elements (so it would look like the result in the irb

a =[one,two,three]
printspecial a # should produce "[one, two, three]" not "onetwothree"
as it # currently does

I tried a.map{|s| s.to_s + ", "
but this gives me [one, two, three,]
with an extra comma trailing the last element

Is there a nice ruby idiom to do what I want (or to simply produce the
necessary string for printing?

Or, do I have to resort to an explicit loop checking for the last
element and not alter it?

···

--
Posted via http://www.ruby-forum.com/.

Chuck Brotman <brotman@nc.rr.com> writes:

Hi,

I want to print an array (of strings, mostly) with commas separating the
elements (so it would look like the result in the irb

[snip]

Is there a nice ruby idiom to do what I want (or to simply produce the
necessary string for printing?

a = ["one", "two", "three"]
puts a.inspect
puts "[" + a.join(", ") + "]"

# >> ["one", "two", "three"]
# >> [one, two, three]

Chuck Brotman wrote:

I want to print an array (of strings, mostly) with commas separating the
elements (so it would look like the result in the irb

You want String#inspect. (irb calls #inspect on the object it's
displaying)

$ irb --simple-prompt

a = ["one","two","three"]

=> ["one", "two", "three"]

puts a.inspect

["one", "two", "three"]
=> nil

···

--
Posted via http://www.ruby-forum.com/\.

You can always try a * ", "

% irb --simple-prompt

a = ["a","b","c"]

=> ["a", "b", "c"]

a * ", "

=> "a, b, c"

As other people have pointed out, you are looking for the inspect method. If
you are wanting to print it
p a
puts a.inspect

are the same thing.

As far as your issue with the comma after the last element, the join method
will do what you were trying to do with map, except it won't put it on the
last element.

a.join(', ') # => "1, 2, 3"

···

On Sun, Jun 13, 2010 at 1:30 PM, Chuck Brotman <brotman@nc.rr.com> wrote:

Hi,

I want to print an array (of strings, mostly) with commas separating the
elements (so it would look like the result in the irb

a =[one,two,three]
printspecial a # should produce "[one, two, three]" not "onetwothree"
as it # currently does

I tried a.map{|s| s.to_s + ", "
but this gives me [one, two, three,]
with an extra comma trailing the last element

Is there a nice ruby idiom to do what I want (or to simply produce the
necessary string for printing?

Or, do I have to resort to an explicit loop checking for the last
element and not alter it?
--
Posted via http://www.ruby-forum.com/\.

If you want it to "look like the result in the irb", you want Array#inspect.

···

On 2010-06-13 11:30:40 -0700, Chuck Brotman said:

Hi,

I want to print an array (of strings, mostly) with commas separating the
elements (so it would look like the result in the irb

a =[one,two,three]
printspecial a # should produce "[one, two, three]" not "onetwothree"
as it # currently does

I tried a.map{|s| s.to_s + ", "
but this gives me [one, two, three,]
with an extra comma trailing the last element

Is there a nice ruby idiom to do what I want (or to simply produce the
necessary string for printing?

Or, do I have to resort to an explicit loop checking for the last
element and not alter it?

--
Rein Henrichs

http://reinh.com

I tried it and that appears to be exactly what I need!!

Thank you very much!!!

Brian Candler wrote:

···

You want String#inspect. (irb calls #inspect on the object it's
displaying)

$ irb --simple-prompt

a = ["one","two","three"]

=> ["one", "two", "three"]

puts a.inspect

["one", "two", "three"]
=> nil

--
Posted via http://www.ruby-forum.com/\.

".inspect", ".join", "*". Of course, there isn't just one way to get
the job done in Ruby!

I appreciate all the tips

Thanks,

Chuck

···

--
Posted via http://www.ruby-forum.com/.

Well, Array#inspect makes use of behavior identical to that of Array#join. You could* implement it as:

class Array
  def inspect
  '[' << map{|item| item.inspect}.join(', ') << ']'
  end
end

Also, Array#join and Array#* are synonyms. So there's way closer to one way than to many. :wink:

* this naive implementation fails on recursive arrays and is less efficient than the one used by Rubinius**, which combines the #map and #join into a single #each: http://github.com/evanphx/rubinius/blob/master/kernel/common/array.rb#L915-926

···

On 2010-06-17 20:08:32 -0700, Chuck Brotman said:

".inspect", ".join", "*". Of course, there isn't just one way to get
the job done in Ruby!

I appreciate all the tips

Thanks,

Chuck

**

Rubinius source-diving is a great way to find out a Ruby reference implementation for something written in C in MRI or YARV Rubies.
--
Rein Henrichs

http://reinh.com