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
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
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
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
(not tested, I'm too lazy/busy to write the tests now)
···
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
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
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).
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!(...)
···
--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.map{|i|i}[1] )
puts "s=%q(#{s})",s.map{|i|i}[1]
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
--
"The best way to predict the future is to invent it."
- Alan Kay
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
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
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
Specific to this example, everyone else is right, and the best way is to consolidate the regex or simply use a condensed split call. However, in the general case, you could do this
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
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
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...)
···
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 .. did not notice that I could use the '|' inside the gsub..... get stuck to [. and...]
···
On 2007-01-12 17:05:34 +0100, Bira <u.alberton@gmail.com> said:
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
...unless you don't want to modify the original object passed as
argument (I'm not sure if this is proper English construct I mean
in that case the caller will see the modifications as well)
···
On 1/12/07, Daniel Martin <martin@snowplow.org> wrote:
Josselin <josselin@wanadoo.fr> writes:
> 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.
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".
Looking at this, and some of the suggested alternatives, I can see how it would get tedious to add more characters to the "replace with space" set.
The use of compact regular expressions doesn't make the code easier to read or maintain.
It may be useful to define the set of special characters, then use that to drive a string transformation.
REPLACE_WITH_SPACE = %w{
\r\n
;
···
,
}.map{ |c| Regexp.new(c) }
class String
def swap_to_spaces
s = self.dupe
REPLACE_WITH_SPACE.each do |re|
s.gsub!( re, ' ')
end
s
end
end