Forum: Ruby on Rails gsub no workie

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave C. (Guest)
on 2006-05-08 21:55
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!
Steve R. (Guest)
on 2006-05-08 23:33
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!
Dave C. (Guest)
on 2006-05-09 00:37
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!
Ezra Z. (Guest)
on 2006-05-09 01:46
(Received via mailing list)
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
Dave C. (Guest)
on 2006-05-09 01:49
> The gsub! with an ! exclamation point modifies the receiver in place.


Rockin! Thanks!
This topic is locked and can not be replied to.