From: John Feminella Date: 2011-02-28T06:06:24+09:00 Subject: Re: Creating my own method > Is there a way that I can write a method f! so that: Yep. Just do this: ==== begin snippet ==== def newify!(s) s.gsub!(/test/, "new") end ==== end snippet ==== Now you'll get: ==== begin snippet ==== s = "here's a test string" newify!(s) puts s # => "here's a new string" ==== end snippet ==== ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Sun, Feb 27, 2011 at 15:45, Paul Sholtz wrote: > Suppose I have a string, say s = "test string" .. the difference between > s.gsub and s.gsub! is whether or not the method modifies the object > itself. That is, s.gsub simply returns a new string w/ the appropriate > modifications, while s.gsub!(/t/,"T") will change s to a new value, to > wit, "TesT sTring".. > > My question is, is there a way that I can create my own "!" methods in > Ruby? > > For instance, if I have: > > def f(s) >  s = "new string" > end > > and then I call: > > s = "test string" > f(s) > puts s > > the output is still "test string".. > > Is there a way that I can write a method f! so that: > > def f!(s) >  s = "new string" > end > > and then when I call: > > s = "test string" > f!(s) > puts s > > the output will be "new string"? > > -- > Posted via http://www.ruby-forum.com/. > >