From: Kristofer M White Date: 2010-12-09T09:48:25+09:00 Subject: Re: Splitting on capital letters On 12/08/2010 06:02 PM, Ralph Shnelvar wrote: > JB> 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 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" -- --- Thanks, Kristofer M White http://www.kmwhite.net https://www.github.com/kmwhite/