Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features
What is a good "Ruby way" to do that?
Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features
What is a good "Ruby way" to do that?
Whether or not this is a good way may be up for debate, but it's *a* way:
irb(main):001:0> "BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=> "Benefits-And-Features"
On 12/08/2010 05:47 PM, Ralph Shnelvar wrote:
Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-FeaturesWhat is a good "Ruby way" to do that?
Ralph Shnelvar wrote in post #967293:
Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-Features
What is a good "Ruby way" to do that?
Second version:
"BenefitsAndFeatures".split(/(?=[A-Z])/).join('-')
--
Posted via http://www.ruby-forum.com/\.
The good Ruby way:
"BenefitsAndFeatures".gsub( /.(?=[[:upper:]])/, '\&-' )
==>"Benefits-And-Features"
On Dec 8, 5:47 pm, Ralph Shnelvar <ral...@dos32.com> wrote:
[Note: parts of this message were removed to make it a legal post.]
Assume I have camelized string like
BenefitsAndFeatures
and I want to convert that to
Benefits-And-FeaturesWhat is a good "Ruby way" to do that?
Whether or not this is a good way may be up for debate, but it's *a* way:
irb(main):001:0>> "BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=>> "Benefits-And-Features"
This is sooo far beyond my skill level.
I'm gonna have to study this one for a while.
Thanks!
Ralph
Here's an ActiveSupport-powered roflscale method:
"BenefitsAndFeatures".underscore.split('_').map(&:capitalize).join('-')
=> "Benefits-And-Features"
On Wed, Dec 8, 2010 at 5:02 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:
> Whether or not this is a good way may be up for debate, but it's *a*
way:irb(main):001:0>>
"BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=>> "Benefits-And-Features"This is sooo far beyond my skill level.
I'm gonna have to study this one for a while.
Thanks!
Ralph
--
Tony Arcieri
Medioh! Kudelski
Simple explanation:
"BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/) returns an
array that was made from the string provided. The pattern for splitting
is a capital letter ([[:upper:]]) followed by one or more lowercase
letters ([[:lower:]]*):
irb(main):001:0> "BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/)
=> ["", "Benefits", "", "And", "", "Features"]
irb(main):002:0>
On that array, we call .delete_if(&:empty?) which will delete any
element from the array if it returns true to the empty check.
irb(main):002:0> _.delete_if(&:empty?)
=> ["Benefits", "And", "Features"]
irb(main):003:0>
On the cleaned array, we re-join into a string putting a dash between
each element.
irb(main):003:0> _.join("-")
=> "Benefits-And-Features"
On 12/08/2010 06:02 PM, Ralph Shnelvar wrote:
> Whether or not this is a good way may be up for debate, but it's *a* way:
irb(main):001:0>> "BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=>> "Benefits-And-Features"This is sooo far beyond my skill level.
I'm gonna have to study this one for a while.
Thanks!
Ralph
--
---
Thanks,
Kristofer M White
http://www.kmwhite.net
maybe try simple first, ie, get lower and up chars and put a dash in bw.
eg
"BenefitsAndFeatures".gsub /([a-z])([A-Z])/ , '\1-\2'
best regards -botp
On Thu, Dec 9, 2010 at 8:02 AM, Ralph Shnelvar <ralphs@dos32.com> wrote:
This is sooo far beyond my skill level.
This might be ridiculous, but ...
("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}" }).reverse.chomp('-').reverse
Sam
On 09/12/10 13:04, Tony Arcieri wrote:
Here's an ActiveSupport-powered roflscale method:
"BenefitsAndFeatures".underscore.split('_').map(&:capitalize).join('-')
=> "Benefits-And-Features"
On Wed, Dec 8, 2010 at 5:02 PM, Ralph Shnelvar<ralphs@dos32.com> wrote:
> Whether or not this is a good way may be up for debate, but it's *a*
way:irb(main):001:0>>
"BenefitsAndFeatures".split(/([[:upper:]][[:lower:]]*)/).delete_if(&:empty?).join("-")
=>> "Benefits-And-Features"This is sooo far beyond my skill level.
I'm gonna have to study this one for a while.
Thanks!
Ralph
Much better! Slap hand on face.
Cheers
robert
On Thu, Dec 9, 2010 at 9:14 AM, botp <botpena@gmail.com> wrote:
On Thu, Dec 9, 2010 at 8:02 AM, Ralph Shnelvar <ralphs@dos32.com> wrote:
This is sooo far beyond my skill level.
maybe try simple first, ie, get lower and up chars and put a dash in bw.
eg
"BenefitsAndFeatures".gsub /([a-z])([A-Z])/ , '\1-\2'
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Sam Duncan wrote in post #967298:
This might be ridiculous, but ...
("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}"
}).reverse.chomp('-').reverseSam
Eliminating the reverse.chomp.reverse:
"BenefitsAndFeatures".gsub(/\w(?=[A-Z])/){|match| "#{match}-"}
the (?=[A-Z]) bit is a positive lookahead; the regex matches a word
character followed by a capital, without making the capital part of the
match.
hth,
Siep
--
Posted via http://www.ruby-forum.com/\.
Very nice =]
On 09/12/10 14:07, Siep Korteling wrote:
Sam Duncan wrote in post #967298:
This might be ridiculous, but ...
("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}"
}).reverse.chomp('-').reverseSam
Eliminating the reverse.chomp.reverse:
"BenefitsAndFeatures".gsub(/\w(?=[A-Z])/){|match| "#{match}-"}
the (?=[A-Z]) bit is a positive lookahead; the regex matches a word
character followed by a capital, without making the capital part of the
match.hth,
Siep
Easier with lookbehind:
irb(main):003:0> s='BenefitsAndFeatures'
=> "BenefitsAndFeatures"
irb(main):004:0> s.gsub(/(?<=[[:lower:]])[[:upper:]]+/, '-\\&')
=> "Benefits-And-Features"
#scan works, too:
irb(main):005:0> s.scan(/[[:upper:]]+[[:lower:]]+/).join('-')
=> "Benefits-And-Features"
Kind regards
robert
On 09.12.2010 02:18, Sam Duncan wrote:
Very nice =]
On 09/12/10 14:07, Siep Korteling wrote:
Sam Duncan wrote in post #967298:
This might be ridiculous, but ...
("BenefitsAndFeatures".gsub(/[A-Z]/) { |c| "-#{c}"
}).reverse.chomp('-').reverseSam
Eliminating the reverse.chomp.reverse:
"BenefitsAndFeatures".gsub(/\w(?=[A-Z])/){|match| "#{match}-"}
the (?=[A-Z]) bit is a positive lookahead; the regex matches a word
character followed by a capital, without making the capital part of the
match.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/