On Sat, 19 Aug 2006, Joshua Muheim wrote:

> What's the fastest way to convert "ThisIsSomeString" to
> "this_is_some_string"?

   harp:~ > cat a.rb
   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

   def camel_case string
     return string if string =~ %r/[A-Z]/ and string !~ %r/_/
     words = string.strip.split %r/\s*_+\s*/
     words.map!{|w| w.downcase.sub(%r/^./){|c| c.upcase}}
     words.join
   end

   s = "ThisIsSomeString"
   p(snake_case(s))

   s = "this_is_some_string"
   p(camel_case(s))


   harp:~ > ruby a.rb
   "this_is_some_string"
   "ThisIsSomeString"

regards.

-a
-- 
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama