[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

DRY gsub...

Josselin

1/12/2007 3:56:00 PM

I wrote the following ruby statements.. I get the result I need , I
tried to DRY it for 2 hours without being successfull ,

d = d.gsub(/\r\n/,' ') # get rid of carriage return
d = d.gsub(/;/,' ') # replace column by space
d = d.gsub(/,/,' ') # replace comma by space
a = d.split(' ') # split into component , space as divider

tfyl

Joss

31 Answers

Bira

1/12/2007 4:06:00 PM

0

On 1/12/07, Josselin <josselin@wanadoo.fr> wrote:
> I wrote the following ruby statements.. I get the result I need , I
> tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider
>
> tfyl

How about a = d.gsub(/\r\n|;|,/,' ').split(' ') ?



--
Bira
http://compexplicita.bl...
http://sinfoniaferida.bl...

Gavin Kistner

1/12/2007 4:07:00 PM

0

Josselin wrote:
> I wrote the following ruby statements.. I get the result I need , I
> tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider

d = d.gsub( /\r\n|[;,]/, ' ' ).split

James Gray

1/12/2007 4:09:00 PM

0

On Jan 12, 2007, at 10:00 AM, Josselin wrote:

> I wrote the following ruby statements.. I get the result I need ,
> I tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider

a = d.split(/(?:\r\n|[;, ])/)

Hope that helps.

James Edward Gray II

Jano Svitok

1/12/2007 4:11:00 PM

0

On 1/12/07, Josselin <josselin@wanadoo.fr> wrote:
> I wrote the following ruby statements.. I get the result I need , I
> tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider

what about:

a = d.gsub(/\r\n|;|,/,' ').split(' ')

or

a = d.split(/\r\n|;|,| /)

(not tested, I'm too lazy/busy to write the tests now)

Gavin Kistner

1/12/2007 4:12:00 PM

0

Josselin wrote:
> I wrote the following ruby statements.. I get the result I need , I
> tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider

BTW, that is already reasonably DRY, in my opinion. Calling the same
method repeatedly but with different parameters is not "repeating
yourself". It would be WET (hrm...Way Extra Toomuchcode) if you had
something like:

d = d.gsub( /\r\n/, ' ' )
e = e.gsub( /\r\n/, ' ' )
f = f.gsub( /\r\n/, ' ' )
g = g.gsub( /\r\n/, ' ' )
etc.

It's just semantics, but IMO what you're asking for is to make your
code more compact (but not necessarily golfing).

Alex Young

1/12/2007 4:12:00 PM

0

Josselin wrote:
> I wrote the following ruby statements.. I get the result I need , I
> tried to DRY it for 2 hours without being successfull ,
>
> d = d.gsub(/\r\n/,' ') # get rid of carriage return
> d = d.gsub(/;/,' ') # replace column by space
> d = d.gsub(/,/,' ') # replace comma by space
> a = d.split(' ') # split into component , space as divider
>
a = d.split(/(\r\n)|([;, ])/)

--
Alex

Bruno Michel

1/12/2007 4:15:00 PM

0

Bira a écrit :
> On 1/12/07, Josselin <josselin@wanadoo.fr> wrote:
>> I wrote the following ruby statements.. I get the result I need , I
>> tried to DRY it for 2 hours without being successfull ,
>>
>> d = d.gsub(/\r\n/,' ') # get rid of carriage return
>> d = d.gsub(/;/,' ') # replace column by space
>> d = d.gsub(/,/,' ') # replace comma by space
>> a = d.split(' ') # split into component , space as divider
>>
>> tfyl
>
> How about a = d.gsub(/\r\n|;|,/,' ').split(' ') ?

Or a = d.split(/\r\n|,|;| /) ?

--
Bruno Michel

Gavin Kistner

1/12/2007 4:20:00 PM

0

James Edward Gray II wrote:
> a = d.split(/(?:\r\n|[;, ])/)

Way more elegant. Way to see beyond the step-by-step process to the end
goal.

Simon Strandgaard

1/12/2007 4:23:00 PM

0

On 1/12/07, Bira <u.alberton@gmail.com> wrote:
> On 1/12/07, Josselin <josselin@wanadoo.fr> wrote:
> > I wrote the following ruby statements.. I get the result I need , I
> > tried to DRY it for 2 hours without being successfull ,
> >
> > d = d.gsub(/\r\n/,' ') # get rid of carriage return
> > d = d.gsub(/;/,' ') # replace column by space
> > d = d.gsub(/,/,' ') # replace comma by space
> > a = d.split(' ') # split into component , space as divider
> >
> > tfyl
>
> How about a = d.gsub(/\r\n|;|,/,' ').split(' ') ?

If you don't care about \r then maybe this

"ab;c,xx\r\nyy zz".scan(/[^ ;,\n]+/)
#=> ["ab", "c", "xx\r", "yy", "zz"]

--
Simon Strandgaard
http://opc...

Josselin

1/12/2007 4:26:00 PM

0

On 2007-01-12 17:11:54 +0100, "Phrogz" <gavin@refinery.com> said:

> Josselin wrote:
>> I wrote the following ruby statements.. I get the result I need , I
>> tried to DRY it for 2 hours without being successfull ,
>>
>> d = d.gsub(/\r\n/,' ') # get rid of carriage return
>> d = d.gsub(/;/,' ') # replace column by space
>> d = d.gsub(/,/,' ') # replace comma by space
>> a = d.split(' ') # split into component , space as divider
>
> BTW, that is already reasonably DRY, in my opinion. Calling the same
> method repeatedly but with different parameters is not "repeating
> yourself". It would be WET (hrm...Way Extra Toomuchcode) if you had
> something like:
>
> d = d.gsub( /\r\n/, ' ' )
> e = e.gsub( /\r\n/, ' ' )
> f = f.gsub( /\r\n/, ' ' )
> g = g.gsub( /\r\n/, ' ' )
> etc.
>
> It's just semantics, but IMO what you're asking for is to make your
> code more compact (but not necessarily golfing).

Thanks to all of you... as a newbie I try to keep this kind of useful
comment in my mind DRY vs WET
(it's now engraved...)