[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

replace spaces with commas?

John Griffiths

11/3/2008 11:58:00 PM

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?
--
Posted via http://www.ruby-....

7 Answers

Peña, Botp

11/4/2008 12:19:00 AM

0

RnJvbTogSm9obiBHcmlmZml0aHMgW21haWx0bzppbmRpZWhlYWRAZ21haWwuY29tXSANCiMgQHMg
PSAiZmRzZHNmLCBkc2Zkc2ZzZCBmZHNmZHNmZCBkZnNmZHMiDQojIHB1dHMgQHMuZ3N1YignLCcs
JycpLnNwbGl0KCAvICAqLyApLmpvaW4oJywnKQ0KIyByZXR1cm5zID0+ICJmZHNkc2YsZHNmZHNm
c2QsZmRzZmRzZmQsZGZzZmRzIg0KDQp0aGF0IGlzIDMgbWV0aHMNCnRyeSBpZiBvbmUgb3IgMiBt
ZXRob2RzIHdvdWxkIGRvLCBlZywNCg0KPiBAcy5nc3ViKC8sKlxzKy8sJywnKQ0KPT4gImZkc2Rz
Zixkc2Zkc2ZzZCxmZHNmZHNmZCxkZnNmZHMiDQo=

John Griffiths

11/4/2008 9:40:00 AM

0

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

will give this a go,



John.
--
Posted via http://www.ruby-....

Henrik Nyh

11/4/2008 10:06:00 AM

0

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

John Griffiths

11/4/2008 11:48:00 AM

0

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 ;-)
--
Posted via http://www.ruby-....

Lloyd Linklater

11/4/2008 2:23:00 PM

0

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.
--
Posted via http://www.ruby-....

Robert Klemme

11/4/2008 3:27:00 PM

0

2008/11/4 Pe=F1a, Botp <botp@delmonte-phil.com>:
> From: John Griffiths [mailto:indiehead@gmail.com]
> # @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

Pablo Q.

11/5/2008 2:42:00 PM

0

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

2008/11/4 John Griffiths <indiehead@gmail.com>

> thanks Pe=F1a, admittedly regular expressions have never been my strong
> point ;-)
>
> will give this a go,
>
>
>
> John.
> --
> Posted via http://www.ruby-....
>
>


--=20
Pablo Q.