On Aug 22, 2007, at 12:35 PM, Daniel Berger wrote:

> 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?
>


cfp:~ > cat a.rb
require 'rubygems'
require 'alib'

p(alib.util.snake_case("CheckHostIP"))


cfp:~ > ruby a.rb
"check_host_ip"



   def snake_case string
     return string unless string =~ %r/[A-Z]/
     string.reverse.scan(%r/[A-Z]+|[^A-Z]*[A-Z]+?/).reverse.map{| 
word| word.reverse.downcase}.join '_'
   end


kind regards.


a @ http://drawohara.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama