[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

de-camelcase a filename

warhero

6/30/2007 5:34:00 PM

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

thanks

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

16 Answers

Swaroop C H

6/30/2007 5:43:00 PM

0

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
--
ion | power your music
Get your own ion right now at www.ion.co.in !

Brett Simmers

6/30/2007 6:33:00 PM

0


> 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


Bill Kelly

6/30/2007 8:17:00 PM

0


From: "Swaroop C H" <swaroopch@gmail.com>
> 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"

Here's another way:

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


Regards,

Bill



John Joyce

6/30/2007 10:26:00 PM

0


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

>
> From: "Swaroop C H" <swaroopch@gmail.com>
>> 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"
>
> 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.

warhero

6/30/2007 10:55:00 PM

0

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 via http://www.ruby-....

Benjamin Kudria

7/2/2007 2:42:00 PM

0

On Saturday, June 30 2007, Bill Kelly wrote:
> From: "Swaroop C H" <swaroopch@gmail.com>
>
> > 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"
>
> 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


Roseanne Zhang

7/2/2007 4:08:00 PM

0

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

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

Bill Kelly

7/2/2007 4:41:00 PM

0

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



Ezra Zygmuntowicz

7/2/2007 5:43:00 PM

0


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)



Drew Olson

7/2/2007 8:48:00 PM

0

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 via http://www.ruby-....