From: Carlos Date: 2006-11-14T19:48:15+09:00 Subject: Re: capitalize bug? spooq wrote: > foo = "do_it_now" > foo.gsub(/_+(.)/, $1.upcase) > => "doNtNow" Inside the block... > > On 11/14/06, Carlos wrote: > >> Jean-pierre Riviere wrote: >> > Hi! >> > >> > I'm a new member here, though I use and enjoy ruby for about three >> > years. There's a beginning to everyuthing, including thing not doing >> the >> > way you think they should, even in ruby! >> > >> > So here's my problem: >> > >> > @nom = wnom.gsub(/_+(.)/, '\1'.upcase) >> >> The parameters to gsub are: 1. a regex, 2. the result of '\1'.upcase >> (that is, '\1'). >> >> > What this code should do is taking the string wnom, and removing >> every _ >> > and at the same time capitalizing the letter immediatly after. >> > >> > for instance do_it_now would become doItNow. One way to translate ruby >> > usage into camel case. >> > >> > but this code does in fact producte doitnow instead of doItNow. >> > >> > Now, I am not blocked, because I have achieved my goal with this code: >> > >> > nom = wnom.gsub(/_+(.)/) { |deb| deb[1, 1].upcase } >> >> If you want to substitute the substring with the result of applying a >> function to it, your only choice is to use the block parameter to gsub, >> yes. (Did the previous sentence parse?) Another possibility is to use >> $1.upcase. >> >> HTH >> -- >> >> >> > >