Forum: Ruby de-camelcase a filename

Posted by Aaron Smith (warhero)
on 2007-06-30 19:34
how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?

thanks
Posted by Swaroop C H (swaroop)
on 2007-06-30 19:43
(Received via mailing list)
On 6/30/07, Aaron Smith <beingthexemplary@gmail.com> 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 :)

Cheers,
Swaroop
Posted by Brett Simmers (Guest)
on 2007-06-30 20:34
(Received via mailing list)
> 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
Posted by Bill Kelly (Guest)
on 2007-06-30 22:18
(Received via mailing list)
From: "Swaroop C H" <swaroopch@gmail.com>
> 
> 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
Posted by John Joyce (Guest)
on 2007-07-01 00:27
(Received via mailing list)
On Jun 30, 2007, at 3:17 PM, Bill Kelly 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.
Posted by Aaron Smith (warhero)
on 2007-07-01 00:55
John Joyce wrote:
> On Jun 30, 2007, at 3:17 PM, Bill Kelly 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!
Posted by Benjamin Kudria (Guest)
on 2007-07-02 16:42
(Received via mailing list)
On Saturday, June 30 2007, Bill Kelly 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
Posted by Roseanne Zhang (roseanne)
on 2007-07-02 18:06
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
Posted by Bill Kelly (Guest)
on 2007-07-02 18:42
(Received via mailing list)
From: "Roseanne Zhang" <roseanne@javaranch.com>
> How about this:
> "FooBarBaz".split(/(?=[A-Z])/).join('_').downcase

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


Regards,

Bill
Posted by Ezra Zygmuntowicz (Guest)
on 2007-07-02 19:44
(Received via mailing list)
On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote:

> From: "Roseanne Zhang" <roseanne@javaranch.com>
>> 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
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
Posted by Drew Olson (dfg59)
on 2007-07-02 22:48
Aaron Smith 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"
Posted by Daniel Berger (Guest)
on 2007-08-22 20:40
(Received via mailing list)
On Jul 2, 11:42 am, Ezra Zygmuntowicz <ezmob...@gmail.com> 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
Posted by Robert Dober (Guest)
on 2007-08-22 21:01
(Received via mailing list)
On 8/22/07, Daniel Berger <djberg96@gmail.com> 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
Posted by David A. Black (Guest)
on 2007-08-22 21:07
(Received via mailing list)
Hi --

On Thu, 23 Aug 2007, Robert Dober 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
Posted by Robert Dober (Guest)
on 2007-08-22 22:05
(Received via mailing list)
On 8/22/07, David A. Black <dblack@rubypal.com> 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
Posted by Simon Krahnke (Guest)
on 2007-08-28 02:35
(Received via mailing list)
* Daniel Berger <djberg96@gmail.com> (2007-08-22) schrieb:

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

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

mfg,                   simon .... l
Posted by ara.t.howard (Guest)
on 2007-08-28 05:52
(Received via mailing list)
On Aug 22, 2007, at 12:35 PM, Daniel Berger 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/
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.