A
Aaron Smith
how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?
thanks
my_test_case.rb?
thanks
how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?
Veterans can provide more succinct ways though
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"
Just be careful of any code that has dependencies on the camelCapsFrom: "Swaroop C H said:I'm a novice at Ruby, but this might help:how can a take a string file name like MyTestCase.rb and change
it to
my_test_case.rb?
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 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.
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"
From: "Roseanne Zhang said:How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
From: "Roseanne Zhang said:How about this:
"FooBarBaz".split(/(?=[A-Z])/).join('_').downcase
Nice... (why didn't I think of that
Aaron said:how can a take a string file name like MyTestCase.rb and change it to
my_test_case.rb?
thanks
From: "Roseanne Zhang said: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
end
From: "Roseanne Zhang" <[email protected]>
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
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
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
Sure that is quite clever, but it will e.g. fail on "AMACAddress"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
* Daniel Berger said:"CheckHostIP".gsub(/\B[A-Z]/, '_\&').downcase => "check_host_i_p"
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?
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.