replace spaces with commas?

J

John Griffiths

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?
 
P

Peña, Botp

RnJvbTogSm9obiBHcmlmZml0aHMgW21haWx0bzppbmRpZWhlYWRAZ21haWwuY29tXSANCiMgQHMg
PSAiZmRzZHNmLCBkc2Zkc2ZzZCBmZHNmZHNmZCBkZnNmZHMiDQojIHB1dHMgQHMuZ3N1YignLCcs
JycpLnNwbGl0KCAvICAqLyApLmpvaW4oJywnKQ0KIyByZXR1cm5zID0+ICJmZHNkc2YsZHNmZHNm
c2QsZmRzZmRzZmQsZGZzZmRzIg0KDQp0aGF0IGlzIDMgbWV0aHMNCnRyeSBpZiBvbmUgb3IgMiBt
ZXRob2RzIHdvdWxkIGRvLCBlZywNCg0KPiBAcy5nc3ViKC8sKlxzKy8sJywnKQ0KPT4gImZkc2Rz
Zixkc2Zkc2ZzZCxmZHNmZHNmZCxkZnNmZHMiDQo=
 
J

John Griffiths

thanks Peña, admittedly regular expressions have never been my strong
point ;-)

will give this a go,



John.
 
H

Henrik Nyh

SnVzdCByZW1lbWJlciB0aGF0IGlmIHlvdSBzcGxpdCBvbiBib3RoIGNvbW1hIGFuZCBzcGFjZSwg
eW91IGxvc2UgdGhlCnJlYXNvbiBtYW55IGhhdmUgZm9yIHVzaW5nIGNvbW1hcyBpbiB0aGUgZmly
c3QgcGxhY2U6IG11bHRpLXdvcmQgdGFncywKbGlrZSAiZm9vIGJhciwgYmF6Ii4KCk9uIFR1ZSwg
Tm92IDQsIDIwMDggYXQgMTA6MzkgQU0sIEpvaG4gR3JpZmZpdGhzIDxpbmRpZWhlYWRAZ21haWwu
Y29tPiB3cm90ZToKPiB0aGFua3MgUGXDsWEsIGFkbWl0dGVkbHkgcmVndWxhciBleHByZXNzaW9u
cyBoYXZlIG5ldmVyIGJlZW4gbXkgc3Ryb25nCj4gcG9pbnQgOy0pCj4KPiB3aWxsIGdpdmUgdGhp
cyBhIGdvLAo+Cj4KPgo+IEpvaG4uCj4gLS0KPiBQb3N0ZWQgdmlhIGh0dHA6Ly93d3cucnVieS1m
b3J1bS5jb20vLgo+Cj4K
 
J

John Griffiths

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 ;-)
 
L

Lloyd Linklater

Henrik said:
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.
 
R

Robert Klemme

2008/11/4 Pe=F1a said:
From: John Griffiths [mailto:[email protected]]
# @s =3D "fdsdsf, dsfdsfsd fdsfdsfd dfsfds"
# puts @s.gsub(',','').split( / */ ).join(',')
# returns =3D> "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"

that is 3 meths
try if one or 2 methods would do, eg,
@s.gsub(/,*\s+/,',')
=3D> "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*/, ','=
)
=3D> "fdsdsf,dsfdsfsd,fdsfdsfd,dfsfds"
irb(main):006:0> "fdsdsf, dsfdsfsd fdsfdsfd dfsfds".gsub(/\s*[\s,]\s*/, ', =
')
=3D> "fdsdsf, dsfdsfsd, fdsfdsfd, dfsfds"

Here's another one

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

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
 
P

Pablo Q.

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

2008/11/4 John Griffiths said:
thanks Pe=F1a, admittedly regular expressions have never been my strong
point ;-)

will give this a go,



John.


--=20
Pablo Q.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top