# Gsub no workie

**URL:** <https://www.ruby-forum.com/t/gsub-no-workie/94600>\
**Category:** Rails\
**Created:** [May 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600 "2006-05-08T19:55:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 8, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600/1 "2006-05-08T19:55:15Z")

</div>

Don’t know why, but cant seem to get this to work for me.

I have a phone number field for an addressbook. In the addressbook model  
I have a before\_update declaration that says the following:

I’d like to be able to remove all special characters (just numbers  
please), but this doesn’t want to work for me.

before\_update :format\_phone

def format\_phone  
self.phone = self.phone.to\_s.gsub(/\D/, “”)  
end

The following works to remove all numbers, so I know it’s reaching the  
before\_update call. 🙂

self.phone = self.phone.to\_s.gsub(/\d/, “”)

Any help is appriciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 8, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600/2 "2006-05-08T21:33:14Z")

</div>

Not sure what you’re trying to do. If the phone is represented as a  
number, then doing that gsub should have no effect. If it’s a string,  
you’re going to have to use:

self.phone\_before\_type\_cast(gsub(/\D/, “”)

Right?

David C. wrote:

> Don’t know why, but cant seem to get this to work for me.
> 
> I have a phone number field for an addressbook. In the addressbook model  
> I have a before\_update declaration that says the following:
> 
> I’d like to be able to remove all special characters (just numbers  
> please), but this doesn’t want to work for me.
> 
> before\_update :format\_phone
> 
> def format\_phone  
> self.phone = self.phone.to\_s.gsub(/\D/, “”)  
> end
> 
> The following works to remove all numbers, so I know it’s reaching the  
> before\_update call. 🙂
> 
> self.phone = self.phone.to\_s.gsub(/\d/, “”)
> 
> Any help is appriciated.
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 8, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600/3 "2006-05-08T22:37:57Z")

</div>

Steve R. wrote:

> Not sure what you’re trying to do.

I don’t think I did either.

I realized after I read your response that I was trying to change the  
wrong thing…

I was really wanting to modify the phone input param.

params[:address][:phone] = params[:address][:phone].gsub(/[\D]/, “”)

Is there a way to do the above with out the this = that?

I feel stupid.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 8, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600/4 "2006-05-08T23:49:26Z")

</div>

> The gsub! with an ! exclamation point modifies the receiver in place.

Rockin! Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [May 8, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/gsub-no-workie/94600/5 "2006-05-08T23:46:24Z")

</div>

On May 8, 2006, at 1:37 PM, David C. wrote:

> params[:address][:phone] = params[:address][:phone].gsub(/[\D]/, “”)
> 
> Is there a way to do the above with out the this = that?
> 
> I feel stupid.
> 
> Thanks for your help!

params[:address][:phone].gsub!(/[\D]/, “”)

The gsub! with an ! exclamation point modifies the receiver in place.

-Ezra
