Forum: Ruby replace spaces with commas?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
760e5c90000057a6860986b69b77c400?d=identicon&s=25 John Griffiths (indiehead)
on 2008-11-04 00:59
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?
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-04 01:21
(Received via mailing list)
From: John Griffiths [mailto:indiehead@gmail.com]
# @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"
760e5c90000057a6860986b69b77c400?d=identicon&s=25 John Griffiths (indiehead)
on 2008-11-04 10:41
thanks Peña, admittedly regular expressions have never been my strong
point ;-)

will give this a go,



John.
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2008-11-04 11:07
(Received via mailing list)
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".
760e5c90000057a6860986b69b77c400?d=identicon&s=25 John Griffiths (indiehead)
on 2008-11-04 12:49
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 ;-)
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2008-11-04 15:24
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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-04 16:29
(Received via mailing list)
2008/11/4 Peña, Botp <botp@delmonte-phil.com>:
> From: John Griffiths [mailto:indiehead@gmail.com]
> # @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
8f27f50c60bbd39d81dc4acd7d3bebdb?d=identicon&s=25 Pablo Q. (pabloq)
on 2008-11-05 15:45
(Received via mailing list)
Just if you want to play  and learn regular expression in ruby, cool
stuff
=> http://rubular.com/

2008/11/4 John Griffiths <indiehead@gmail.com>
This topic is locked and can not be replied to.