Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > replace spaces with commas?

Reply
Thread Tools

replace spaces with commas?

 
 
John Griffiths
Guest
Posts: n/a
 
      11-03-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Peña, Botp
Guest
Posts: n/a
 
      11-04-2008
RnJvbTogSm9obiBHcmlmZml0aHMgW21haWx0bzppbmRpZWhlYW RAZ21haWwuY29tXSANCiMgQHMg
PSAiZmRzZHNmLCBkc2Zkc2ZzZCBmZHNmZHNmZCBkZnNmZHMiDQ ojIHB1dHMgQHMuZ3N1YignLCcs
JycpLnNwbGl0KCAvICAqLyApLmpvaW4oJywnKQ0KIyByZXR1cm 5zID0+ICJmZHNkc2YsZHNmZHNm
c2QsZmRzZmRzZmQsZGZzZmRzIg0KDQp0aGF0IGlzIDMgbWV0aH MNCnRyeSBpZiBvbmUgb3IgMiBt
ZXRob2RzIHdvdWxkIGRvLCBlZywNCg0KPiBAcy5nc3ViKC8sKl xzKy8sJywnKQ0KPT4gImZkc2Rz
Zixkc2Zkc2ZzZCxmZHNmZHNmZCxkZnNmZHMiDQo=

 
Reply With Quote
 
 
 
 
John Griffiths
Guest
Posts: n/a
 
      11-04-2008
thanks Peña, admittedly regular expressions have never been my strong
point

will give this a go,



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

 
Reply With Quote
 
Henrik Nyh
Guest
Posts: n/a
 
      11-04-2008
SnVzdCByZW1lbWJlciB0aGF0IGlmIHlvdSBzcGxpdCBvbiBib3 RoIGNvbW1hIGFuZCBzcGFjZSwg
eW91IGxvc2UgdGhlCnJlYXNvbiBtYW55IGhhdmUgZm9yIHVzaW 5nIGNvbW1hcyBpbiB0aGUgZmly
c3QgcGxhY2U6IG11bHRpLXdvcmQgdGFncywKbGlrZSAiZm9vIG JhciwgYmF6Ii4KCk9uIFR1ZSwg
Tm92IDQsIDIwMDggYXQgMTA6MzkgQU0sIEpvaG4gR3JpZmZpdG hzIDxpbmRpZWhlYWRAZ21haWwu
Y29tPiB3cm90ZToKPiB0aGFua3MgUGXDsWEsIGFkbWl0dGVkbH kgcmVndWxhciBleHByZXNzaW9u
cyBoYXZlIG5ldmVyIGJlZW4gbXkgc3Ryb25nCj4gcG9pbnQgOy 0pCj4KPiB3aWxsIGdpdmUgdGhp
cyBhIGdvLAo+Cj4KPgo+IEpvaG4uCj4gLS0KPiBQb3N0ZWQgdm lhIGh0dHA6Ly93d3cucnVieS1m
b3J1bS5jb20vLgo+Cj4K

 
Reply With Quote
 
John Griffiths
Guest
Posts: n/a
 
      11-04-2008
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-forum.com/.

 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      11-04-2008
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-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-04-2008
2008/11/4 Pe=F1a, Botp <>:
> From: John Griffiths [private.php?do=newpm&u=]
> # @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

 
Reply With Quote
 
Pablo Q.
Guest
Posts: n/a
 
      11-05-2008
Just if you want to play and learn regular expression in ruby, cool stuff
=3D> http://rubular.com/

2008/11/4 John Griffiths <>

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



--=20
Pablo Q.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57