[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

comma separating output from array.to_s

jansenh

12/2/2006 8:45:00 PM

hi comp.lang.ruby

what is the ruby-way of comma separating the output from array.to_s?

[I have been playing with ruby for some time now, but I often find
myself solving problems in a Java/C# manner... That's me, a
non-idiomatic ruby programmer... ]

regards, Henning

17 Answers

William James

12/2/2006 8:51:00 PM

0


jansenh wrote:
> hi comp.lang.ruby
>
> what is the ruby-way of comma separating the output from array.to_s?
>
> [I have been playing with ruby for some time now, but I often find
> myself solving problems in a Java/C# manner... That's me, a
> non-idiomatic ruby programmer... ]
>
> regards, Henning

>> [2,4,6,8].join(',')
=> "2,4,6,8"

Devin Mullins

12/2/2006 8:52:00 PM

0

jansenh wrote:
> what is the ruby-way of comma separating the output from array.to_s?
ri Array#join

Devin

Gregory Brown

12/2/2006 8:57:00 PM

0

On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
> hi comp.lang.ruby
>
> what is the ruby-way of comma separating the output from array.to_s?

some_array.join(",")

but if you are thinking of doing CSV output, you should not use a
naive approach like this, since it will likely produce malformed CSV
when dealing with quoted text.

There is the CSV standard library, or if you'd like better performance
and a cleaner interface, FasterCSV available from RubyForge.

matt

12/2/2006 9:01:00 PM

0

jansenh <henning.jansen@gmail.com> wrote:

> what is the ruby-way of comma separating the output from array.to_s?

You might be looking for Array#join. In fact, I believe Array#to_s *is*
Array#join.

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

William James

12/2/2006 10:33:00 PM

0

Gregory Brown wrote:
> On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
> > hi comp.lang.ruby
> >
> > what is the ruby-way of comma separating the output from array.to_s?
>
> some_array.join(",")
>
> but if you are thinking of doing CSV output, you should not use a
> naive approach like this, since it will likely produce malformed CSV
> when dealing with quoted text.


>> puts [22,'He said, "No!"'].map{|x| x=x.to_s
>> x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
22,"He said, ""No!"""

William James

12/2/2006 10:36:00 PM

0

Gregory Brown wrote:
> On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
> > hi comp.lang.ruby
> >
> > what is the ruby-way of comma separating the output from array.to_s?
>
> some_array.join(",")
>
> but if you are thinking of doing CSV output, you should not use a
> naive approach like this, since it will likely produce malformed CSV
> when dealing with quoted text.


C:\>irb --prompt xmp
puts [22,'He said, "No!"'].map{|x| x=x.to_s
x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
22,"He said, ""No!"""

jansenh

12/3/2006 12:20:00 AM

0


William James wrote:
> >> [2,4,6,8].join(',')
> => "2,4,6,8"


That's it! :-) Thanx to all of you.

regards, Henning

Gregory Brown

12/3/2006 1:07:00 AM

0

On 12/2/06, William James <w_a_x_man@yahoo.com> wrote:
> Gregory Brown wrote:
> > On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
> > > hi comp.lang.ruby
> > >
> > > what is the ruby-way of comma separating the output from array.to_s?
> >
> > some_array.join(",")
> >
> > but if you are thinking of doing CSV output, you should not use a
> > naive approach like this, since it will likely produce malformed CSV
> > when dealing with quoted text.
>
>
> >> puts [22,'He said, "No!"'].map{|x| x=x.to_s
> >> x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
> 22,"He said, ""No!"""

Yeah, now deal with edge cases, and try to run that on 100k rows :)

William James

12/3/2006 4:22:00 AM

0

Gregory Brown wrote:
> On 12/2/06, William James <w_a_x_man@yahoo.com> wrote:
> > Gregory Brown wrote:
> > > On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
> > > > hi comp.lang.ruby
> > > >
> > > > what is the ruby-way of comma separating the output from array.to_s?
> > >
> > > some_array.join(",")
> > >
> > > but if you are thinking of doing CSV output, you should not use a
> > > naive approach like this, since it will likely produce malformed CSV
> > > when dealing with quoted text.
> >
> >

> > puts [22,'He said, "No!"'].map{|x| x=x.to_s
> > x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
> > 22,"He said, ""No!"""
>
> Yeah, now deal with edge cases, and try to run that on 100k rows :)

It's easy to handle all cases since CSV is a simple format;
no pompous prolixity is needed:

puts ['x',' y ','He said, "No!"'].map{|x| x=x.to_s
x =~ /["\n]|^\s|\s$/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
x," y ","He said, ""No!"""

If that won't handle 100k rows, then fasterCsv probably won't either.

Ken Bloom

12/3/2006 5:21:00 AM

0

On Sat, 02 Dec 2006 20:22:20 -0800, William James wrote:

> Gregory Brown wrote:
>> On 12/2/06, William James <w_a_x_man@yahoo.com> wrote:
>> > Gregory Brown wrote:
>> > > On 12/2/06, jansenh <henning.jansen@gmail.com> wrote:
>> > > > hi comp.lang.ruby
>> > > >
>> > > > what is the ruby-way of comma separating the output from array.to_s?
>> > >
>> > > some_array.join(",")
>> > >
>> > > but if you are thinking of doing CSV output, you should not use a
>> > > naive approach like this, since it will likely produce malformed CSV
>> > > when dealing with quoted text.
>> >
>> >
>
>> > puts [22,'He said, "No!"'].map{|x| x=x.to_s
>> > x =~ /["\n]/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
>> > 22,"He said, ""No!"""
>>
>> Yeah, now deal with edge cases, and try to run that on 100k rows :)
>
> It's easy to handle all cases since CSV is a simple format;
> no pompous prolixity is needed:

Most of use like pompous proxility as it helps us understand what the heck
is going on. If I ran across your CSV regexp line in code I was debugging,
I wouldn't know what it meant.

> puts ['x',' y ','He said, "No!"'].map{|x| x=x.to_s
> x =~ /["\n]|^\s|\s$/ ? '"' + x.gsub(/"/,'""') + '"' : x }.join(',')
> x," y ","He said, ""No!"""
>
> If that won't handle 100k rows, then fasterCsv probably won't either.



--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...