From: dblack@... Date: 2002-09-16T12:08:15+09:00 Subject: Re: camelCaseTo_ruby_case.rb ?? 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. > 2. How will you handle quotation of various kinds? > > "decamel replaces 'camelVar' with 'camel_var'".decamel > > # ==> "decamel replaces 'camel_var' with 'camel_var'" > > The fact that %{ } can be nested means you'll need something more than > regexes... Yes, a full-featured decameler would have to get over that hump (ha ha). Mine is very mini and quick-n-dirty, probably to the point of pointlessness. I imagine there's literally no way to do it perfectly, without harnessing an actual Ruby parser. Actually a decameler was one of things considered for the Austin Ruby codefest. Since we opted for scanf, the decamler project is still up for grabs.... (Or has it been done? I never did research the original question :-) David -- David Alan Black | Register for RubyConf 2002! home: dblack@candle.superlink.net | November 1-3 work: blackdav@shu.edu | Seattle, WA, USA Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com