Convert "ThisIsSomeString" to "this_is_some_string"?

Hi all

What’s the fastest way to convert “ThisIsSomeString” to
“this_is_some_string”?

Thanks for help,
Joshua

On Sat, 19 Aug 2006, Joshua M. 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

Thanks a lot for your help. :slight_smile:

Joshua M. wrote:

Hi all

What’s the fastest way to convert “ThisIsSomeString” to
“this_is_some_string”?

Thanks for help,
Joshua

p “ThisIsSomeString”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’’)
p “this_is_some_string”.split(’
’).map{|w|w.capitalize}.join

lopex

Marcin MielżyÅ?ski wrote:

Joshua M. wrote:

Hi all

What’s the fastest way to convert “ThisIsSomeString” to
“this_is_some_string”?

Thanks for help,
Joshua

p “ThisIsSomeString”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’’)
p “this_is_some_string”.split(’
’).map{|w|w.capitalize}.join

lopex

syntax error near unexpected token `(’

:frowning:

On Sun, 20 Aug 2006, [UTF-8] Marcin MielżyÅ~Dski wrote:

p “ThisIsSomeString”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’’)
p “this_is_some_string”.split(’
’).map{|w|w.capitalize}.join

irb(main):001:0>
“FooBAR”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’_’)
=> “foo”

it’s trickier than it looks.

-a

Joshua M. wrote:

Hi all

What’s the fastest way to convert “ThisIsSomeString” to
“this_is_some_string”?

I believe this is the fastest way, though it may not catch all cases
(numbers and such)

def lowerize(str) # ??
str.gsub(/([a-z])([A-Z])/, ‘\1_\2’).downcase
end

Cheers,
Daniel

On Sun, 20 Aug 2006, Daniel S. wrote:

str.gsub(/([a-z])([A-Z])/, ‘\1_\2’).downcase
end

Cheers,
Daniel

depending on what you want, the snake_case code i posted works
differently:

irb(main):025:0> lowerize ‘BARFoo’
=> “barfoo”

irb(main):026:0> snake_case ‘BARFoo’
=> “bar_foo”

neither is right - just fyi.

-a

Joshua M. wrote:

p “ThisIsSomeString”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’’)
p “this_is_some_string”.split(’
’).map{|w|w.capitalize}.join

lopex

syntax error near unexpected token `(’

:frowning:

works for me, maybe some typo ?

lopex

How about this one?

class MyString < String
def snake_case(group = true)
return self if self == ‘’
if group
if self =~ /\b(?:[A-Z]+)(?:[A-Z][a-z])/
gsub(/\b([A-Z]+)([A-Z][a-z])/,’\1_\2’).gsub(/\s/,’’).downcase
else
gsub(/([a-zA-Z])([A-Z])/,’\1
\2’).gsub(/\s/,’’).downcase
end
else
gsub(/([a-zA-Z])(?=[A-Z])/,’\1
\2’).gsub(/\s/,’_’).downcase
end
end
end

a = MyString.new(“fooBar”)
puts a.snake_case
b = MyString.new(“FOOBar”)
puts b.snake_case
c = MyString.new(“FOOOOBar”)
puts c.snake_case

When ran:

foo_bar
foo_bar
foooo_bar

Kirk H.

If you have Rails installed:

require ‘action_pack’
“ThisIsSomeString”.underscore
=> “this_is_some_string”

– Scott

[email protected] wrote:

irb(main):025:0> lowerize ‘BARFoo’
=> “barfoo”

irb(main):026:0> snake_case ‘BARFoo’
=> “bar_foo”

neither is right - just fyi.

Correct. Your solution has a flaw, too.

“fooBar”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’_’) => “bar”

Lets see if we can solve this without peeking at the Rails source :slight_smile:

Cheers,
Daniel

On Sun, 20 Aug 2006, Daniel S. wrote:

Correct. Your solution has a flaw, too.

“fooBar”.scan(/[A-Z][a-z]+/).map{|w|w.downcase}.join(’_’) => “bar”

good catch!

Lets see if we can solve this without peeking at the Rails source :slight_smile:

here’s a start:

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]+?|[^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

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
FooBar foo_bar
Foo foo
],

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

 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 a
Started

Finished in 0.001527 seconds.

8 tests, 8 assertions, 0 failures, 0 errors

cheers.

-a

[email protected] wrote:

here’s a start:

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]+?|[^A-Z]+/).reverse.map{|word|
word.reverse.downcase}.join ‘_’
end

Can we agree that “ABCde” => “ab_cde”?

I still get an error when the original string has underscores in it:

“foo_Bar” => “foo__bar”
“foo_BAR” => “foo__bar”

Too tired to figure out a solution right now, I’ll get back to you in
the morning (that’ll probably be your night.)

Cheers,
Daniel

Joshua M. wrote:

Hi all

What’s the fastest way to convert “ThisIsSomeString” to
“this_is_some_string”?

Thanks for help,
Joshua


Posted via http://www.ruby-forum.com/.

p [“FooBar”,“fooBar”,“FOOBar”,“FooBAR”].map{|s|
s.gsub(/.(?=[A-Z])/,‘&_’).downcase }

[“foo_bar”, “foo_bar”, “f_o_o_bar”, “foo_b_a_r”]

On Sun, 20 Aug 2006, Daniel S. wrote:

Can we agree that “ABCde” => “ab_cde”?
sure. it’s arbitrary i realize - but i make sense to me! :wink:

I still get an error when the original string has underscores in it:

“foo_Bar” => “foo__bar”
“foo_BAR” => “foo__bar”

Too tired to figure out a solution right now, I’ll get back to you in the
morning (that’ll probably be your night.)

ok. i actually use this method in some production code so a better impl
would
be nice!

here’s the latest, which fixes bug above:

harp:~ > cat a.rb
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/+/,’_’)
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
FooBar foo_bar
Foo foo
Foo_Bar foo_bar
foo_Bar foo_bar
foo_BAR foo_bar
],

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

 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 a
Started

Finished in 0.003574 seconds.

11 tests, 11 assertions, 0 failures, 0 errors

-a

[email protected] 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/+/,’_’)

I’m not sure you want to label these as bugs:

“FOO_Bar” => “fo_o_bar”
“FOO_bar” => “fo_o_bar”

But this probably is:

“FOO_BAR” => “fo_o_bar”

Cheers,
Daniel

Daniel S. wrote:

[email protected] 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

On Aug 19, 2006, at 10:49 AM, Joshua M. wrote:

I like to do string.split(/(?=[A-Z])/).map { |s| s.downcase }.join(’_’)

On 19-aug-2006, at 19:10, Scott wrote:

If you have Rails installed:

require ‘action_pack’
“ThisIsSomeString”.underscore
=> “this_is_some_string”

require ‘active_support’ is enough.