de-camelcase a filename

A

Aaron Smith

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

thanks
 
S

Swaroop C H

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 :)

Cheers,
Swaroop
 
B

Brett Simmers

Veterans can provide more succinct ways though :)

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
 
B

Bill Kelly

From: "Swaroop C H said:
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"

Here's another way:

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


Regards,

Bill
 
J

John Joyce

From: "Swaroop C H said:
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"

Here's another way:

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


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.
 
A

Aaron Smith

John said:
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!
 
B

Benjamin Kudria

From: "Swaroop C H said:
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"

Here's another way:

irb(main):078:0> "FooBarBaz".split(/(?=[A-Z])/).map{|w|
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
 
E

Ezra Zygmuntowicz

From: "Roseanne Zhang said:
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase

Nice... (why didn't I think of that :)


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 Zygmuntowicz
-- Lead Rails Evangelist
-- (e-mail address removed)
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
 
D

Drew Olson

Aaron said:
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"
 
D

Daniel Berger

From: "Roseanne Zhang said:
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
Nice... (why didn't I think of that :)

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

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
 
R

Robert Dober

From: "Roseanne Zhang" <[email protected]>
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
Nice... (why didn't I think of that :)

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

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

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
 
D

David A. Black

Hi --

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

From: "Roseanne Zhang" <[email protected]>
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase

Nice... (why didn't I think of that :)

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

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

I guess there can be no general solution, I would like
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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober

Hi --

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

From: "Roseanne Zhang" <[email protected]>
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase

Nice... (why didn't I think of that :)

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

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

I guess there can be no general solution, I would like
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
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
 
S

Simon Krahnke

* Daniel Berger said:
"CheckHostIP".gsub(/\B[A-Z]/, '_\&').downcase => "check_host_i_p"

"CheckHostIP".gsub(/\B[A-Z]+/, '_\&').downcase => "check_host_ip"

mfg, simon .... l
 
A

ara.t.howard

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top