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-forum.com/.
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-forum.com/.
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
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?
--
ion | power your music
Get your own ion right now at www.ion.co.in !
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-forum.com/\.
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
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
From: "Swaroop C H" <swaroopch@gmail.com>
On 6/30/07, Aaron Smith <beingthexemplary@gmail.com> wrote:
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.
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
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
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"
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-forum.com/\.
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
--
Posted via http://www.ruby-forum.com/.
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
Nice... (why didn't I think of that ![]()
Regards,
Bill
From: "Roseanne Zhang" <roseanne@javaranch.com>
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)
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('_').downcaseNice... (why didn't I think of that
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
On Jul 2, 11:42 am, Ezra Zygmuntowicz <ezmob...@gmail.com> wrote:
On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote:
> From: "Roseanne Zhang" <rosea...@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
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 8/22/07, Daniel Berger <djberg96@gmail.com> wrote:
On Jul 2, 11:42 am, Ezra Zygmuntowicz <ezmob...@gmail.com> wrote:
> On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote:
>
> > From: "Roseanne Zhang" <rosea...@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
> endBoth 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'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn
Hi --
From: "Roseanne Zhang" <rosea...@javaranch.com>
How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcaseNice... (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
endBoth 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
On Thu, 23 Aug 2007, Robert Dober wrote:
On 8/22/07, Daniel Berger <djberg96@gmail.com> wrote:
On Jul 2, 11:42 am, Ezra Zygmuntowicz <ezmob...@gmail.com> wrote:
On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote:
--
* 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)
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
On 8/22/07, David A. Black <dblack@rubypal.com> wrote:
Hi --
On Thu, 23 Aug 2007, Robert Dober wrote:
> On 8/22/07, Daniel Berger <djberg96@gmail.com> wrote:
>> On Jul 2, 11:42 am, Ezra Zygmuntowicz <ezmob...@gmail.com> wrote:
>>> On Jul 2, 2007, at 9:41 AM, Bill Kelly wrote:
>>>
>>>> From: "Roseanne Zhang" <rosea...@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
>>
>> 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_ipHere'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
endDavid
--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn