DRY gsub...

J

Josselin

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
 
B

Bira

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(' ') ?
 
P

Phrogz

Josselin said:
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
 
J

James Edward Gray II

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
 
J

Jan Svitok

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

Phrogz

Josselin said:
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).
 
A

Alex Young

Josselin said:
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)|([;, ])/)
 
S

Simon Strandgaard

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"]
 
J

Josselin

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...)
 
P

Phrogz

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

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

Except that there's no need for the non-capturing group, so
(simplifying, not golfing):

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

Unless, of course, you have a string like this:
d = "foo; bar\r\n\r\nwhee"
and you only wanted [ "foo", "bar", "whee" ], in which case:
a = d.split(/(?:\r\n|[;, ])+/)

\Man, I'm the king of multiple posting today
\\Fark slashies ftw!
\\\Back to work
 
D

Daniel Martin

Josselin said:
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's wrong with:

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

Or do you need d to be mangled as before?

Although I probably would do something even shorter like this:

a = d.split(/[;,\s]+/)

However, for certain inputs that won't give exactly the same as your
initial multi-step procedure.

Also, any time you write:

d = d.gsub(...)

You're probably better off with:

d.gsub!(...)
 
P

Phrogz

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

Hope that helps.

Out of interest, what does the ?: do in there? I've googled, etc, honest!

It says, "Please don't capture the stuff in parentheses here, because
that changes what split returns".

irb(main):001:0> s = "a b,c"
=> "a b,c"
irb(main):002:0> s.split( /( |,)/ )
=> ["a", " ", "b", ",", "c"]
irb(main):003:0> s.split( /(?: |,)/ )
=> ["a", "b", "c"]
 
S

Simon Strandgaard

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

Hope that helps.

Out of interest, what does the ?: do in there? I've googled, etc, honest!

(?: ) is a non-capturing group

its not necessary here
"ab;c,xx\r\nyy zz".split(/\r\n|[;, ]/)
#=> ["ab", "c", "xx", "yy", "zz"]
 
R

Robert Klemme

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

Hope that helps.

Out of interest, what does the ?: do in there? I've googled, etc, honest!

It's a non capturing group. You cannot get the characters from it which
is at times more efficient because the RX engine does not need to do the
bookkeeping and storing of the group.

robert
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top