Convert "ThisIsSomeString" to "this_is_some_string"?

Daniel Schierbeck wrote:

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

This is what I got:

   class String
     def snake_case
       return self unless self =~ /[A-Z]/
       split('_').map do |part|
         break "" if part.empty?
         reversed_words = part.reverse.scan(/[A-Z]+|[^A-Z]*[A-Z]+?|[^A-Z]+/)
         reversed_words.reverse.map do |word|
           word.reverse.downcase
         end.join('_')
       end.join('_')
     end
   end

   if $0 == __FILE__
     require 'test/unit'

     class SnakeCaseTest < Test::Unit::TestCase
       i = 0

       %w{Foo_bar Foo_Bar FOO_Bar FOO_bar FOO_BAR foo_Bar
       fooBar FooBar FooBAR FOOBar}.each do |str|
         define_method("test_#{i += 1}") do
           assert_equal "foo_bar", str.snake_case,
                                   "could not handle #{str}"
         end
       end

       %w{_foobar __foobar _foobar_ __foobar__
       foo_bar _foo_bar_ __foo_bar__}.each do |str|
         define_method("test_#{i += 1}") do
           assert_equal str, str.snake_case
         end
       end
     end
   end

Cheers,
Daniel

···

ara.t.howard@noaa.gov wrote:

<snip and integrate into my test>

this is what i have - anyone have improvments? comments on camel_case?

harp:~ > cat a.rb
def snake_case string
   return string unless string =~ /[A-Z]/
   string.split('_').map do |part|
     break "" if part.empty?
     reversed_words = part.reverse.scan(/[A-Z]+|[^A-Z]*[A-Z]+?|[^A-Z]+/)
     reversed_words.reverse.map do |word|
       word.reverse.downcase
     end.join('_')
   end.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

if $0 == __FILE__
   require 'test/unit'
   require 'enumerator'

   class T < Test::Unit::TestCase
     tests = {
       "snake_case" => %w[
         ThisIsSomeString this_is_some_string
         FOOBar foo_bar
         FOO_BAR foo_bar
         FOO_Bar foo_bar
         FOO_bar foo_bar
         Foo foo
         FooBAR foo_bar
         FooBar foo_bar
         Foo_Bar foo_bar
         Foo_bar foo_bar
         fooBar foo_bar
         foo_BAR foo_bar
         foo_Bar foo_bar
       ],

       "camel_case" => %w[
         foo Foo
         foo_bar FooBar
         foo_bar_foobar FooBarFoobar
         this_is_some_string ThisIsSomeString
       ]
     }

     tests.each do |meth, list|
       testno = -1
       list.each_slice(2) do |arg, expected|
         define_method "test_#{ meth }_#{ testno += 1 }" do
           actual = send meth, arg
           assert_equal expected, actual
         end
       end
     end
   end
end

harp:~ > ruby a.rb
Loaded suite snake_camel
Started
.................
Finished in 0.002831 seconds.

17 tests, 17 assertions, 0 failures, 0 errors

thanks for the input!

cheers.

-a

···

On Sun, 20 Aug 2006, Daniel Schierbeck wrote:

Daniel Schierbeck wrote:

ara.t.howard@noaa.gov wrote:

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

This is what I got:

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