Replace spaces with commas?

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?

From: John G. [mailto:[email protected]]

@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”

thanks Peña, admittedly regular expressions have never been my strong
point :wink:

will give this a go,

John.

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”.

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.

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

2008/11/4 Peña, Botp [email protected]:

From: John G. [mailto:[email protected]]

@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

Just if you want to play and learn regular expression in ruby, cool
stuff
=> http://rubular.com/

2008/11/4 John G. [email protected]