# DRY up consecutive gsub's - How to

**URL:** <https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198>\
**Category:** Ruby\
**Created:** [April 10, 2009, 3:31am UTC](https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198 "2009-04-10T03:31:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 10, 2009, 3:31am UTC](https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198/1 "2009-04-10T03:31:44Z")

</div>

Hi,

I have a csv file suitable for an Excel display. The file has column-  
names on certain row.

In order to process subsequent rows I want to refer to their columns  
symbolically using the column-names. That requires the column-names to  
be cleaned up in order to create valid Ruby symbols.

The following worked fine:  
if col\_name  
col\_name.gsub!(/[\s]/, “”)  
col\_name.gsub!(/[&]/, “And”)  
col\_name.gsub!(/[#]/, “Number”)  
end

Each of the following one-liners failed:  
col\_name.gsub!(/[\s]/, “”). gsub!(/[&]/, “And”).gsub!(/[#]/,  
“Number”) if col\_name  
col\_name.gsub(/[\s]/, “”). gsub(/[&]/, “And”).gsub(/[#]/, “Number”)  
if col\_name

Can either of the one-liners be made to work?

The code is show in a larger (easier to read) context at  
[http://www.pastie.org/442456](http://www.pastie.org/442456)

Thanks in Advance,  
Richard

.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 10, 2009, 3:56am UTC](https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198/2 "2009-04-10T03:56:57Z")

</div>

RichardOnRails wrote:

> Each of the following one-liners failed:  
> col\_name.gsub!(/[\s]/, “”). gsub!(/[&]/, “And”).gsub!(/[#]/,  
> “Number”) if col\_name

col\_name = col\_name.to\_s.gsub(/[\s]/, “”). gsub(/[&]/,  
“And”).gsub(/[#]/,  
“Number”)

If you don’t mind col\_name shifting from a nil to a blank.

Also, the [] brackets add no value - both [\s] and \s are a one-width  
match.

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 10, 2009, 3:59am UTC](https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198/3 "2009-04-10T03:59:18Z")

</div>

On Fri, Apr 10, 2009 at 3:30 AM, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if col_name
> 
> ```
> 
> Can either of the one-liners be made to work?
> 
> The code is show in a larger (easier to read) context at [http://www.pastie.org/442456](http://www.pastie.org/442456)
> 
> Thanks in Advance,  
> Richard

Here is a oneliner  
col.gsub!(/[\s&#]/){ |x| x == “#” ? “Number” : x == “&” ? “And” : “” }  
just to show that your code is much better.  
Why do you dislike it, it is readable and simple.  
If you are worried about changes, you might potentially factor that out

[/\s/, “”, “&”, “And”, "#', “Number”].each\_slice( 2 ) do | reg, rep |  
col.gsub! reg, rep  
end

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [April 10, 2009, 4:50am UTC](https://www.ruby-forum.com/t/dry-up-consecutive-gsubs-how-to/165198/4 "2009-04-10T04:50:44Z")

</div>

On Apr 9, 9:25 pm, RichardOnRails  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if col_name
> 
> ```
> 
> Can either of the one-liners be made to work?
> 
> The code is show in a larger (easier to read) context athttp://www.pastie.org/442456
> 
> Thanks in Advance,  
> Richard
> 
> .

Hi Philip and Robert,

Thank you both for great responses.

First off, I still wanted to know what was the flaw in my one-liner;  
I found two:

1. I needed carefully placed parenthesis
2. The parenthesized version failed because I just discovered) gsub!  
returns nil when no change is made. Bad idea for my purpose; no doubt  
Matz saw a good reason for it.

Bob, your version worked fine and fulfills my goal  
Ditto for your version, Robert.  
For both, I appended “if if col\_name” to handle unnamed columns.

With two great choices, my aesthetics lead me to Robert’s version  
because:

1. I like the x.gsub! … better than x = x.gsub …
2. The single gsub! with the choices laid out in order packs expanding  
it for additional cases with the least typing.

Again, thank you both very much for your help.

Best wishes,  
Richard

Most of all, I like one-liners much better than 5-liners for  
readability.
