From: Joel VanderWerf Date: 2002-09-16T12:25:28+09:00 Subject: Re: camelCaseTo_ruby_case.rb ?? dblack@candle.superlink.net wrote: > Hi -- > > On Mon, 16 Sep 2002, Joel VanderWerf wrote: > > >>Two comments: >> >>1. #gsub! has big teeth and will bite you: >> >> "FooBar".decamel # ==> nil >> >>It seems ok with #gsub instead of the first occurrence of #gsub!. > > > How strange. When I do this in irb, I get nil, as you say. But when > I run my little script: > > #!/usr/bin/ruby -wp > > BEGIN { > class String > def decamel > gsub!(/\b[^\WA-Z]+[A-Z].*?\b/) do |x| > x.gsub!(/[A-Z][^\WA-Z]+/) {|s| "_" + s.downcase } > x.gsub!(/[A-Z][A-Z]+/) {|s| "_" + s } > x > end > end > end > } > > $_.decamel > > > I get this (where odd-numbered lines are typed in and even ones are the > output): > > someMethodOrOther > some_method_or_other > SomeClass > SomeClass > > which is what I wanted. I can't quite puzzle through why there would > be that difference. I guess -p calls #print, which prints $_, and ignores the value of the expression.