Forum: Ruby replace spaces with commas?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John G. (Guest)
on 2008-11-04 01:59
Been looking at the Acts As Taggable On Steriods plugin, by default it
makes each word separated by a comma a tag but I really wanted an easier
way to handle this so each word gets a comma put between it so it
becomes a tag and the user doesn't have to worry about this.

so i built this, which seems to work kinda well,

@s = "fdsdsf, dsfdsfsd fdsfdsfd dfsfds"
puts @s.gsub(',','').split( /  */ ).join(',')

returns => "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"

what do you think or is there an easier method?
Peña, Botp (Guest)
on 2008-11-04 02:21
(Received via mailing list)
From: John G. [mailto:removed_email_address@domain.invalid]
# @s = "fdsdsf, dsfdsfsd fdsfdsfd dfsfds"
# puts @s.gsub(',','').split( /  */ ).join(',')
# returns => "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"

that is 3 meths
try if one or 2 methods would do, eg,

> @s.gsub(/,*\s+/,',')
=> "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"
John G. (Guest)
on 2008-11-04 11:41
thanks Peña, admittedly regular expressions have never been my strong
point ;-)

will give this a go,



John.
Henrik -. (Guest)
on 2008-11-04 12:07
(Received via mailing list)
Just remember that if you split on both comma and space, you lose the
reason many have for using commas in the first place: multi-word tags,
like "foo bar, baz".
John G. (Guest)
on 2008-11-04 13:49
agree, but what i'm after is the same functionality as flickr has when
tagging photos, each word separated by a space is converted to a tag.

good stuff ;-)
Lloyd L. (Guest)
on 2008-11-04 16:24
Henrik --- wrote:
> Just remember that if you split on both comma and space, you lose the
> reason many have for using commas in the first place: multi-word tags,
> like "foo bar, baz".

I would think that you might also want to take care for csv files that
have string fields.

e.g.

"here we go","something here","commas, inside a field",123

Differentiating internally is extra work.  I am not sure if it matters
in your case as your example may well be greatly pared down, but caveat
emptor.
Robert K. (Guest)
on 2008-11-04 17:29
(Received via mailing list)
2008/11/4 Peña, Botp <removed_email_address@domain.invalid>:
> From: John G. [mailto:removed_email_address@domain.invalid]
> # @s = "fdsdsf, dsfdsfsd fdsfdsfd dfsfds"
> # puts @s.gsub(',','').split( /  */ ).join(',')
> # returns => "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"
>
> that is 3 meths
> try if one or 2 methods would do, eg,
>
>> @s.gsub(/,*\s+/,',')
> => "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"

This won't retain empty words. If you have to do this you can do

irb(main):005:0> "fdsdsf, dsfdsfsd fdsfdsfd dfsfds".gsub(/\s*[\s,]\s*/,
',')
=> "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"
irb(main):006:0> "fdsdsf, dsfdsfsd fdsfdsfd dfsfds".gsub(/\s*[\s,]\s*/,
', ')
=> "fdsdsf, dsfdsfsd, fdsfdsfd, dfsfds"

Here's another one

irb(main):008:0> "fdsdsf, dsfdsfsd fdsfdsfd dfsfds".gsub(/\W+/, ',')
=> "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"
irb(main):009:0> "fdsdsf, dsfdsfsd fdsfdsfd dfsfds".gsub(/\W+/, ', ')
=> "fdsdsf, dsfdsfsd, fdsfdsfd, dfsfds"

Cheers

robert
Pablo Q. (Guest)
on 2008-11-05 16:45
(Received via mailing list)
Just if you want to play  and learn regular expression in ruby, cool
stuff
=> http://rubular.com/

2008/11/4 John G. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.