J
Jan Svitok
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!(...)
...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)