De-camelcase a filename

how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?

thanks

On 6/30/07, Aaron S. [email protected] wrote:

how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?

I’m a novice at Ruby, but this might help:

irb(main):013:0> ‘MyTestCase’.gsub(/[A-Z]/) {
irb(main):014:1* |p| ‘_’ + p.downcase
irb(main):015:1> }
=> “_my_test_case”

then:

irb(main):020:0> “_my_test_case”[1…-1]
=> “my_test_case”

Veterans can provide more succinct ways though :slight_smile:

Cheers,
Swaroop

Veterans can provide more succinct ways though :slight_smile:

I wouldn’t consider myself a veteran yet, but here’s how Rails does it:

camel_cased_word.to_s.gsub(/::/, ‘/’).
gsub(/([A-Z]+)([A-Z][a-z])/,’\1_\2’).
gsub(/([a-z\d])([A-Z])/,’\1_\2’).
tr("-", “_”).
downcase

That also changes :: to /, so it’s handy for translating a module name
to a file path. It’s not exactly more succinct, but you can cut it down
as you see fit.

Brett

From: “Swaroop C H” [email protected]

then:

irb(main):020:0> “_my_test_case”[1…-1]
=> “my_test_case”

Here’s another way:

irb(main):078:0> “FooBarBaz”.split(/(?=[A-Z])/).map{|w|
w.downcase}.join("_")
=> “foo_bar_baz”

Regards,

Bill

John J. wrote:

On Jun 30, 2007, at 3:17 PM, Bill K. wrote:

=> “_my_test_case”

Regards,

Bill

Just be careful of any code that has dependencies on the camelCaps
version!
You might even write a conditional require statement to check for
both versions.

Thanks everyone!

On Jun 30, 2007, at 3:17 PM, Bill K. wrote:

=> “_my_test_case”

Regards,

Bill

Just be careful of any code that has dependencies on the camelCaps
version!
You might even write a conditional require statement to check for
both versions.

How about this:
“FooBarBaz”.split(/(?=[A-Z])/).join(’_’).downcase

On Saturday, June 30 2007, Bill K. wrote:

irb(main):015:1> }
w.downcase}.join("_") => “foo_bar_baz”
After seeing this split -> map -> join in my scripts, I came up with
smj:

class String
def smj(s, j=s, &b)
r = self.split(s).map(&b)
j ? r.join(j) : r
end
end

So the above would become:
“FooBarBaz”.smj(/(?=[A-Z])/, ‘_’) { |w| w.downcase } #=> “foo_bar_baz”

I love Pe…Ruby! I love Ruby!

-Ben Kudria

From: “Roseanne Z.” [email protected]

How about this:
“FooBarBaz”.split(/(?=[A-Z])/).join(’_’).downcase

Nice… (why didn’t I think of that :slight_smile:

Regards,

Bill

Aaron S. wrote:

how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?

thanks

Possibly overkill, but you can use ActiveSupport, it has this
functionality built in:

irb(main):001:0> require ‘rubygems’
=> true
irb(main):002:0> require ‘active_support’
=> true
irb(main):003:0> “ThisIsATest”.underscore
=> “this_is_a_test”

On Jul 2, 2007, at 9:41 AM, Bill K. wrote:

From: “Roseanne Z.” [email protected]

How about this:
“FooBarBaz”.split(/(?=[A-Z])/).join(’_’).downcase

Nice… (why didn’t I think of that :slight_smile:

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

Cheers-

– Ezra Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

On 8/22/07, Daniel B. [email protected] wrote:

“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

I guess there can be no general solution, I would like
HostIP --> host_ip
but
AHostIP --> a_host_ip

maybe you can use a dictonary of Uppercase Abbreviations as a
preparatory step?

Robert

On Jul 2, 11:42 am, Ezra Z. [email protected] wrote:

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

Hi –

On Thu, 23 Aug 2007, Robert D. wrote:

This is the cleanest I’ve come up with:

HostIP --> host_ip
but
AHostIP --> a_host_ip

Here’s how ActiveSupport does it:

def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, ‘/’).
gsub(/([A-Z]+)([A-Z][a-z])/,’\1_\2’).
gsub(/([a-z\d])([A-Z])/,’\1_\2’).
tr("-", “_”).
downcase
end

David

On 8/22/07, David A. Black [email protected] wrote:

“FooBarBaz”.split(/(?=[A-Z])/).join(’_’).downcase
end

 camel_cased_word.to_s.gsub(/::/, '/').
   gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
   gsub(/([a-z\d])([A-Z])/,'\1_\2').
   tr("-", "_").
   downcase

end

David

Sure that is quite clever, but it will e.g. fail on “AMACAddress”
which I want to have as a_mac_address, well but that was a little side
issue of Dan, I guess this is the best solution for OP’s problem.
I just wanted to point Dan to the fact that he would need a dictionary.

Cheers

Robert

“CheckHostIP”.gsub(/\B[A-Z]/, ‘_&’).downcase => “check_host_i_p”

“CheckHostIP”.gsub(/\B[A-Z]+/, ‘_&’).downcase => “check_host_ip”

mfg, simon … l

On Aug 22, 2007, at 12:35 PM, Daniel B. 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs