From: Daniel Berger Date: 2007-08-23T03:35:15+09:00 Subject: Re: de-camelcase a filename On Jul 2, 11:42 am, Ezra Zygmuntowicz wrote: > On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote: > > > From: "Roseanne Zhang" > >> How about this: > >> "FooBarBaz".split(/(?=[A-Z])/).join('_').downcase > > > Nice... (why didn't I think of that :) > > This is the cleanest I've come up with: > > class String > # "FooBar".snake_case #=> "foo_bar" > def snake_case > gsub(/\B[A-Z]/, '_\&').downcase > end > end Both of these solutions have a problem with back to back caps. For example: "CheckHostIP".gsub(/\B[A-Z]/, '_\&').downcase => "check_host_i_p" But I want "check_host_ip". It's probably a simple tweak, but I'm having trouble finding it at the moment. Suggestions? Thanks, Dan