Hey guys, As a novice to both Rails and the Ruby language itself I have found myself in another budge. I have a membership signup page where I want to remove all characters from the phone number so that I can format and search phone numbers easily. I have the following code: -------------------- # Processes new member info and saves member. def create @member = Member.new(params[:member]) @member.signed_up_on = Time.now phone = @member.phone_number @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") .... --------------------- This code doesn''t generate any errors but the number appears in the database without the formatting removed. What is the best way to accomplish this? I''m no fool.. well ok I''m a fool when it comes to ruby. Your advice to my stupid questions will be much appreciated. Thanks! - Jim
On Sep 10, 2005, at 4:07 AM, Jim Jeffers wrote:> Hey guys, > As a novice to both Rails and the Ruby language itself I have found > myself in another budge. > > I have a membership signup page where I want to remove all characters > from the phone number so that I can format and search phone numbers > easily. I have the following code: > > -------------------- > # Processes new member info and saves member. > def create > > @member = Member.new(params[:member]) > @member.signed_up_on = Time.now > > phone = @member.phone_number > @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") > > .... > > --------------------- > > This code doesn''t generate any errors but the number appears in the > database without the formatting removed. What is the best way to > accomplish this? I''m no fool.. well ok I''m a fool when it comes to > ruby. Your advice to my stupid questions will be much appreciated. > Thanks! > > - JimTry: @member.phone_number = phone.gsub /[^\d]/, '''' -- Scott Barron Lunchbox Software http://lunchboxsoftware.com http://lunchroom.lunchboxsoftware.com
String.delete(args) will remove only the characters that intersect - like set theory in maths. (in other words, in: ''hello'' and ''el'', only ''el'' intersect.) So, you''d be wanting just one argument, with all the chars you didn''t want in it". Eg: member.phone_number.delete!("^" + (0..9).to_a.join) (0..9) gives you a range, to_a yields an array of this range explicitly, and then join joins it all into one string. The ^ character makes it "not"... therefore, we''re deleting "all characters not a number, which I think is what you wanted to do :) Julian. On 10/09/2005, at 6:07 PM, Jim Jeffers wrote:> Hey guys, > As a novice to both Rails and the Ruby language itself I have found > myself in another budge. > > I have a membership signup page where I want to remove all > characters from the phone number so that I can format and search > phone numbers easily. I have the following code: > > -------------------- > # Processes new member info and saves member. > def create > > @member = Member.new(params[:member]) > @member.signed_up_on = Time.now > > phone = @member.phone_number > @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") > > .... > > --------------------- > > This code doesn''t generate any errors but the number appears in the > database without the formatting removed. What is the best way to > accomplish this? I''m no fool.. well ok I''m a fool when it comes to > ruby. Your advice to my stupid questions will be much > appreciated. Thanks! > > - Jim > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Julian, Can you explain the logic behind ''to_a.join ''? - Jim On Sep 10, 2005, at 6:48 AM, Julian Leviston wrote:> String.delete(args) > > will remove only the characters that intersect - like set theory in > maths. > > (in other words, in: ''hello'' and ''el'', only ''el'' intersect.) > > So, you''d be wanting just one argument, with all the chars you > didn''t want in it". > > Eg: > > member.phone_number.delete!("^" + (0..9).to_a.join) > > (0..9) gives you a range, to_a yields an array of this range > explicitly, and then join joins it all into one string. > > The ^ character makes it "not"... therefore, we''re deleting "all > characters not a number, which I think is what you wanted to do :) > > Julian. > > > > On 10/09/2005, at 6:07 PM, Jim Jeffers wrote: > > > >> Hey guys, >> As a novice to both Rails and the Ruby language itself I have >> found myself in another budge. >> >> I have a membership signup page where I want to remove all >> characters from the phone number so that I can format and search >> phone numbers easily. I have the following code: >> >> -------------------- >> # Processes new member info and saves member. >> def create >> >> @member = Member.new(params[:member]) >> @member.signed_up_on = Time.now >> >> phone = @member.phone_number >> @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") >> >> .... >> >> --------------------- >> >> This code doesn''t generate any errors but the number appears in >> the database without the formatting removed. What is the best way >> to accomplish this? I''m no fool.. well ok I''m a fool when it >> comes to ruby. Your advice to my stupid questions will be much >> appreciated. Thanks! >> >> - Jim >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
This brings up another question. If I''m reformatting the phone number in a specific way to store it in the database how do I validate it''s uniqueness since the number has now been altered? - Jim On Sep 10, 2005, at 5:00 AM, Scott Barron wrote:> > On Sep 10, 2005, at 4:07 AM, Jim Jeffers wrote: > > >> Hey guys, >> As a novice to both Rails and the Ruby language itself I have >> found myself in another budge. >> >> I have a membership signup page where I want to remove all >> characters from the phone number so that I can format and search >> phone numbers easily. I have the following code: >> >> -------------------- >> # Processes new member info and saves member. >> def create >> >> @member = Member.new(params[:member]) >> @member.signed_up_on = Time.now >> >> phone = @member.phone_number >> @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") >> >> .... >> >> --------------------- >> >> This code doesn''t generate any errors but the number appears in >> the database without the formatting removed. What is the best way >> to accomplish this? I''m no fool.. well ok I''m a fool when it >> comes to ruby. Your advice to my stupid questions will be much >> appreciated. Thanks! >> >> - Jim >> > > Try: > > @member.phone_number = phone.gsub /[^\d]/, '''' > > -- > Scott Barron > Lunchbox Software > http://lunchboxsoftware.com > http://lunchroom.lunchboxsoftware.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
In your customer model: --- before_validation :normalize_phone_number validates_uniqueness_of :phone_number def normalize_phone_number phone_number = phone_number.gsub(/[^\d]/, '''') unless phone_number.nil? end --- What the gsub call does is performs a global substitute on any match in the phone number for /[^\d]/ (first param) and substitutes it for '''', so it effectively deletes any matches. The regular expression /[^\d]/ matches any single character (specified by the contents of []) that is NOT (the ^) numeric (the \d). You could also use (for clarity) gsub(/^[0-9]/, '''') - this achieve the same result. Hope that helps! -DF On 9/11/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> This brings up another question. If I''m reformatting the phone > number in a specific way to store it in the database how do I > validate it''s uniqueness since the number has now been altered? > > - Jim > > On Sep 10, 2005, at 5:00 AM, Scott Barron wrote: > > > > > On Sep 10, 2005, at 4:07 AM, Jim Jeffers wrote: > > > > > >> Hey guys, > >> As a novice to both Rails and the Ruby language itself I have > >> found myself in another budge. > >> > >> I have a membership signup page where I want to remove all > >> characters from the phone number so that I can format and search > >> phone numbers easily. I have the following code: > >> > >> -------------------- > >> # Processes new member info and saves member. > >> def create > >> > >> @member = Member.new(params[:member]) > >> @member.signed_up_on = Time.now > >> > >> phone = @member.phone_number > >> @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") > >> > >> .... > >> > >> --------------------- > >> > >> This code doesn''t generate any errors but the number appears in > >> the database without the formatting removed. What is the best way > >> to accomplish this? I''m no fool.. well ok I''m a fool when it > >> comes to ruby. Your advice to my stupid questions will be much > >> appreciated. Thanks! > >> > >> - Jim > >> > > > > Try: > > > > @member.phone_number = phone.gsub /[^\d]/, '''' > > > > -- > > Scott Barron > > Lunchbox Software > > http://lunchboxsoftware.com > > http://lunchroom.lunchboxsoftware.com > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
WOW. Thanks David! Sorry guys I''m just not getting the hang of Ruby or Rails everytime I post a question I get a really easy solution. Hopefuly I''ll get the hang of this soon. That definitely helped David. - Jim On Sep 10, 2005, at 3:11 PM, David Felstead wrote:> In your customer model: > --- > before_validation :normalize_phone_number > > validates_uniqueness_of :phone_number > > def normalize_phone_number > phone_number = phone_number.gsub(/[^\d]/, '''') unless > phone_number.nil? > end > --- > > What the gsub call does is performs a global substitute on any match > in the phone number for /[^\d]/ (first param) and substitutes it for > '''', so it effectively deletes any matches. The regular expression > /[^\d]/ matches any single character (specified by the contents of []) > that is NOT (the ^) numeric (the \d). You could also use (for > clarity) gsub(/^[0-9]/, '''') - this achieve the same result. > > Hope that helps! > > -DF > > On 9/11/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >> This brings up another question. If I''m reformatting the phone >> number in a specific way to store it in the database how do I >> validate it''s uniqueness since the number has now been altered? >> >> - Jim >> >> On Sep 10, 2005, at 5:00 AM, Scott Barron wrote: >> >> >>> >>> On Sep 10, 2005, at 4:07 AM, Jim Jeffers wrote: >>> >>> >>> >>>> Hey guys, >>>> As a novice to both Rails and the Ruby language itself I have >>>> found myself in another budge. >>>> >>>> I have a membership signup page where I want to remove all >>>> characters from the phone number so that I can format and search >>>> phone numbers easily. I have the following code: >>>> >>>> -------------------- >>>> # Processes new member info and saves member. >>>> def create >>>> >>>> @member = Member.new(params[:member]) >>>> @member.signed_up_on = Time.now >>>> >>>> phone = @member.phone_number >>>> @member.phone_number = phone.delete("(",")",".","-","a-z","A- >>>> Z") >>>> >>>> .... >>>> >>>> --------------------- >>>> >>>> This code doesn''t generate any errors but the number appears in >>>> the database without the formatting removed. What is the best way >>>> to accomplish this? I''m no fool.. well ok I''m a fool when it >>>> comes to ruby. Your advice to my stupid questions will be much >>>> appreciated. Thanks! >>>> >>>> - Jim >>>> >>>> >>> >>> Try: >>> >>> @member.phone_number = phone.gsub /[^\d]/, '''' >>> >>> -- >>> Scott Barron >>> Lunchbox Software >>> http://lunchboxsoftware.com >>> http://lunchroom.lunchboxsoftware.com >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Another question, Ok, here is another basic question: The code you suggested did not work rather I had to do: def normalize_phone_number self.phone_number = self.phone_number.gsub(/[^\d]/, '''') unless self.phone_number.nil? end Can someone explain to me: 1. Why did I need to use self. if most examples that I''ve seen so far have no need for including that. 2. This is not related but what is the difference and purpose of declarind attr_accessible and attr_accessor in a model? - Jim On Sep 10, 2005, at 3:11 PM, David Felstead wrote:> In your customer model: > --- > before_validation :normalize_phone_number > > validates_uniqueness_of :phone_number > > def normalize_phone_number > phone_number = phone_number.gsub(/[^\d]/, '''') unless > phone_number.nil? > end > --- > > What the gsub call does is performs a global substitute on any match > in the phone number for /[^\d]/ (first param) and substitutes it for > '''', so it effectively deletes any matches. The regular expression > /[^\d]/ matches any single character (specified by the contents of []) > that is NOT (the ^) numeric (the \d). You could also use (for > clarity) gsub(/^[0-9]/, '''') - this achieve the same result. > > Hope that helps! > > -DF > > On 9/11/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >> This brings up another question. If I''m reformatting the phone >> number in a specific way to store it in the database how do I >> validate it''s uniqueness since the number has now been altered? >> >> - Jim >> >> On Sep 10, 2005, at 5:00 AM, Scott Barron wrote: >> >> >>> >>> On Sep 10, 2005, at 4:07 AM, Jim Jeffers wrote: >>> >>> >>> >>>> Hey guys, >>>> As a novice to both Rails and the Ruby language itself I have >>>> found myself in another budge. >>>> >>>> I have a membership signup page where I want to remove all >>>> characters from the phone number so that I can format and search >>>> phone numbers easily. I have the following code: >>>> >>>> -------------------- >>>> # Processes new member info and saves member. >>>> def create >>>> >>>> @member = Member.new(params[:member]) >>>> @member.signed_up_on = Time.now >>>> >>>> phone = @member.phone_number >>>> @member.phone_number = phone.delete("(",")",".","-","a-z","A- >>>> Z") >>>> >>>> .... >>>> >>>> --------------------- >>>> >>>> This code doesn''t generate any errors but the number appears in >>>> the database without the formatting removed. What is the best way >>>> to accomplish this? I''m no fool.. well ok I''m a fool when it >>>> comes to ruby. Your advice to my stupid questions will be much >>>> appreciated. Thanks! >>>> >>>> - Jim >>>> >>>> >>> >>> Try: >>> >>> @member.phone_number = phone.gsub /[^\d]/, '''' >>> >>> -- >>> Scott Barron >>> Lunchbox Software >>> http://lunchboxsoftware.com >>> http://lunchroom.lunchboxsoftware.com >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> Another question, > > Ok, here is another basic question: > > The code you suggested did not work rather I had to do: > > def normalize_phone_number > self.phone_number = self.phone_number.gsub(/[^\d]/, '''') unless > self.phone_number.nil? > end > > Can someone explain to me: > > 1. Why did I need to use self. if most examples that I''ve seen so far > have no need for including that.If you don''t use self for the assignment, ruby will create a local variable instead of using the phone_number= method. The following line works: self.phone_number = phone_number.gsub(/[^\d]/, '''') unless phone_number.nil? or I prefer: self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_number> > - Jim
I did, didn''t I? using a regexp is a much better idea, tho. I''ll try to explain it a bit better. In ruby, you can "chain" methods together. so I can type: member.phone_number (gives me the phone number, which is a String object in this case) and I can equally write: member.phone_number.delete!(''s'') which will take that string, and run the delete method on it, removing all instances of "s" from the phone number. I can do that anywhere. So, (0..9) is a Range object (as an example, get up an irb session and type (0..9).class and you''ll get Range as the result. Type (0..9).methods and you''ll be given a list of the methods that it responds to) The range is not explicit, though. To make it explicit, I convert it to an array with "to_a" (try this out in an irb session) (0..9).to_a This gives me:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] which is an array of numbers explicitly. Now I want to join them all together into a string. An array can take the join method, which if we look up the documentation for by using ri, we get the follwoing explanation: Returns a string created by converting each element of the array to a string, separated by _sep_ so (0..9).to_a.join gives you this: "0123456789", which when you put the NOT character in front of it "^", is exactly what you want to delete :) I hope that''s a better explanation. The gsub and regexp [/\D/] is much more concise and rubyish, though. Julian On 11/09/2005, at 4:08 AM, Jim Jeffers wrote:> Julian, > > Can you explain the logic behind ''to_a.join ''? > > - Jim > > On Sep 10, 2005, at 6:48 AM, Julian Leviston wrote: > > > >> String.delete(args) >> >> will remove only the characters that intersect - like set theory >> in maths. >> >> (in other words, in: ''hello'' and ''el'', only ''el'' intersect.) >> >> So, you''d be wanting just one argument, with all the chars you >> didn''t want in it". >> >> Eg: >> >> member.phone_number.delete!("^" + (0..9).to_a.join) >> >> (0..9) gives you a range, to_a yields an array of this range >> explicitly, and then join joins it all into one string. >> >> The ^ character makes it "not"... therefore, we''re deleting "all >> characters not a number, which I think is what you wanted to do :) >> >> Julian. >> >> >> >> On 10/09/2005, at 6:07 PM, Jim Jeffers wrote: >> >> >> >> >>> Hey guys, >>> As a novice to both Rails and the Ruby language itself I have >>> found myself in another budge. >>> >>> I have a membership signup page where I want to remove all >>> characters from the phone number so that I can format and search >>> phone numbers easily. I have the following code: >>> >>> -------------------- >>> # Processes new member info and saves member. >>> def create >>> >>> @member = Member.new(params[:member]) >>> @member.signed_up_on = Time.now >>> >>> phone = @member.phone_number >>> @member.phone_number = phone.delete("(",")",".","-","a-z","A-Z") >>> >>> .... >>> >>> --------------------- >>> >>> This code doesn''t generate any errors but the number appears in >>> the database without the formatting removed. What is the best >>> way to accomplish this? I''m no fool.. well ok I''m a fool when it >>> comes to ruby. Your advice to my stupid questions will be much >>> appreciated. Thanks! >>> >>> - Jim >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hey Jim, validates_uniqueness_of will do it for you at the database level - ie before saving it, but if you''re worried, use a before_validate hook, and put in your data modification there. Julian. On 11/09/2005, at 7:04 AM, Jim Jeffers wrote:> This brings up another question. If I''m reformatting the phone > number in a specific way to store it in the database how do I > validate it''s uniqueness since the number has now been altered? > > - Jim >
Actually it''s because he''s coding in the model, and self refers to the class of object he''s coding in. self.phone_number means the phone number of whatever class of object you''re using. Julian. On 11/09/2005, at 11:50 AM, Stephen Veit wrote:> On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >> Another question, >> >> Ok, here is another basic question: >> >> The code you suggested did not work rather I had to do: >> >> def normalize_phone_number >> self.phone_number = self.phone_number.gsub(/[^\d]/, '''') unless >> self.phone_number.nil? >> end >> >> Can someone explain to me: >> >> 1. Why did I need to use self. if most examples that I''ve seen so far >> have no need for including that. >> > > If you don''t use self for the assignment, ruby will create a local > variable instead of using the phone_number= method. > > The following line works: > self.phone_number = phone_number.gsub(/[^\d]/, '''') unless > phone_number.nil? > > or I prefer: > > self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_number > > >> >> - Jim >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 11.9.2005, at 5.07, Julian Leviston wrote:> Actually it''s because he''s coding in the model, and self refers to > the class of object he''s coding in. > > self.phone_number means the phone number of whatever class of > object you''re using. > > Julian. > > >> On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: >> >> self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_numberHow about phone_number.gsub!(/[^\d]/, '''') if phone_number (note the exclamation mark) //jarkko>> >> >> >>> >>> - Jim >>> >>> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko: What''s the purpose of the exclamation mark in this case? Julian: Thanks so much for your explanation it really helped. - Jim On Sep 11, 2005, at 10:09 AM, Jarkko Laine wrote:> > On 11.9.2005, at 5.07, Julian Leviston wrote: > > > >> Actually it''s because he''s coding in the model, and self refers to >> the class of object he''s coding in. >> >> self.phone_number means the phone number of whatever class of >> object you''re using. >> >> Julian. >> >> >> >> >>> On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: >>> >>> self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_number >>> >>> > > How about > > phone_number.gsub!(/[^\d]/, '''') if phone_number > > (note the exclamation mark) > > //jarkko > > > > >>> >>> >>> >>> >>> >>>> >>>> - Jim >>>> >>>> >>>> >>>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Jim, Command names ending with "!" are commands that modify the existing object, as opposed to creating a copy and then modifying it. Example: a = "foo" b = a.gsub("o", "a") puts b # => "faa" puts a # => "foo" #original, "a", not modified. --------------------------------- a = "foo" a.gsub!("o", "a") puts a # => "faa" # original modified The "!" makes the code cleaner, and eliminates the overhead of copying an object, so alhough equivalent to "a = a.gsub("o", "a")", it's a tiny, tiny, tiny bit faster. Good practice, in any case. Hope this clears this up (or if I'm wrong, please correct me!). Jacob On 9/11/05, Jim Jeffers <rails@donttrustthisguy.com> wrote:> Jarkko: > What's the purpose of the exclamation mark in this case? > > Julian: > Thanks so much for your explanation it really helped. > > - Jim > > On Sep 11, 2005, at 10:09 AM, Jarkko Laine wrote: > > > > > > On 11.9.2005, at 5.07, Julian Leviston wrote: > > > > > > > >> Actually it's because he's coding in the model, and self refers to > >> the class of object he's coding in. > >> > >> self.phone_number means the phone number of whatever class of > >> object you're using. > >> > >> Julian. > >> > >> > >> > >> > >>> On 9/10/05, Jim Jeffers <rails@donttrustthisguy.com> wrote: > >>> > >>> self.phone_number = phone_number.gsub(/[^\d]/, '') if phone_number > >>> > >>> > > > > How about > > > > phone_number.gsub!(/[^\d]/, '') if phone_number > > > > (note the exclamation mark) > > > > //jarkko > > > > > > > > > >>> > >>> > >>> > >>> > >>> > >>>> > >>>> - Jim > >>>> > >>>> > >>>> > >>>> > >>> _______________________________________________ > >>> Rails mailing list > >>> Rails@lists.rubyonrails.org > >>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >>> > >>> > >>> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Jim - typically in ruby, a method call suffixed by an exclamation mark indicates that the method will modify the object that called the method. In this case, gsub (without the exclamation mark) returns a copy of the ''phone_number'' variable with the substitutions performed on it, and the original ''phone_number'' stays intact (well, in this case it doesn''t, because we assign the new variable over the top of it, but without the ''phone_number ='' bit at the start, it would remain unchanged). With gsub!, the modification to ''phone_number'' is done in place, i.e. it changes the object that called the method directly. The exclamation mark is a convention to indicate that the method is ''dangerous'' - it modifies its caller. Hope that helps, -David Felstead On 9/12/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> Jarkko: > What''s the purpose of the exclamation mark in this case? > > Julian: > Thanks so much for your explanation it really helped. > > - Jim > > On Sep 11, 2005, at 10:09 AM, Jarkko Laine wrote: > > > > > > On 11.9.2005, at 5.07, Julian Leviston wrote: > > > > > > > >> Actually it''s because he''s coding in the model, and self refers to > >> the class of object he''s coding in. > >> > >> self.phone_number means the phone number of whatever class of > >> object you''re using. > >> > >> Julian. > >> > >> > >> > >> > >>> On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >>> > >>> self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_number > >>> > >>> > > > > How about > > > > phone_number.gsub!(/[^\d]/, '''') if phone_number > > > > (note the exclamation mark) > > > > //jarkko > > > > > > > > > >>> > >>> > >>> > >>> > >>> > >>>> > >>>> - Jim > >>>> > >>>> > >>>> > >>>> > >>> _______________________________________________ > >>> Rails mailing list > >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >>> > >>> > >>> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks Jacob and David that helps a ton! Is the other convention of using a ''?'' at the end of a method to test if it exists by returning a boolean value? For instance is that what is going on in this code?: " ... unless phone_number.nil? " Thanks a lot you guys are helping me out a lot. - Jim On Sep 11, 2005, at 3:13 PM, David Felstead wrote:> Hi Jim - typically in ruby, a method call suffixed by an exclamation > mark indicates that the method will modify the object that called the > method. In this case, gsub (without the exclamation mark) returns a > copy of the ''phone_number'' variable with the substitutions performed > on it, and the original ''phone_number'' stays intact (well, in this > case it doesn''t, because we assign the new variable over the top of > it, but without the ''phone_number ='' bit at the start, it would remain > unchanged). > > With gsub!, the modification to ''phone_number'' is done in place, i.e. > it changes the object that called the method directly. > > The exclamation mark is a convention to indicate that the method is > ''dangerous'' - it modifies its caller. > > Hope that helps, > > -David Felstead > > On 9/12/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > > >> Jarkko: >> What''s the purpose of the exclamation mark in this case? >> >> Julian: >> Thanks so much for your explanation it really helped. >> >> - Jim >> >> On Sep 11, 2005, at 10:09 AM, Jarkko Laine wrote: >> >> >> >> >>> >>> On 11.9.2005, at 5.07, Julian Leviston wrote: >>> >>> >>> >>> >>> >>>> Actually it''s because he''s coding in the model, and self refers to >>>> the class of object he''s coding in. >>>> >>>> self.phone_number means the phone number of whatever class of >>>> object you''re using. >>>> >>>> Julian. >>>> >>>> >>>> >>>> >>>> >>>> >>>>> On 9/10/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: >>>>> >>>>> self.phone_number = phone_number.gsub(/[^\d]/, '''') if phone_number >>>>> >>>>> >>>>> >>>>> >>> >>> How about >>> >>> phone_number.gsub!(/[^\d]/, '''') if phone_number >>> >>> (note the exclamation mark) >>> >>> //jarkko >>> >>> >>> >>> >>> >>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>> >>>>>> - Jim >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>> _______________________________________________ >>>>> Rails mailing list >>>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>>> >>>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Yep! nil? means "is it true or false that the previous object is nil?" Julian. On 12/09/2005, at 12:09 PM, Jim Jeffers wrote:> Thanks Jacob and David that helps a ton! > > Is the other convention of using a ''?'' at the end of a method to > test if it exists by returning a boolean value? For instance is > that what is going on in this code?: > > " ... unless phone_number.nil? " > > Thanks a lot you guys are helping me out a lot. > > - Jim > > On Sep 11, 2005, at 3:13 PM, David Felstead wrote:_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sure, no problem!> Is the other convention of using a '?' at the end of a method to test > if it exists by returning a boolean value? For instance is that what > is going on in this code?: > > " ... unless phone_number.nil? "Yes. Well, not always testing if they exist, but they test something. "?" methods always return booleans, and usually answer a question: "foo".is_a? String # => true "foo".frozen? # => false Jacob _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails