Replace spaces with commas?


#1

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?


#2

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”


#3

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

will give this a go,

John.


#4

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


#5

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.


#6

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:


#7

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


#8

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