I've created a web framework that uses the following function:
def to_class(text)
text.capitalize
text.gsub(/(_|-)/, '')
end
To turn directory names that are snake_cased
or hyphen-cased
into PascalCased
class names for your project.
Problem is, the function only removed _
and -
, and doesn't capitalize the next letter. Using .capitalize
, or .upcase
, is there a way to achieve making your snake/hyphen_/-cased
names into proper PascalCased
class names?
This splits the _-cased string into an array; capitalizes every member and glues the array back to a string:
Your code does not work for several reasons:
text.capitalize!
ortext = text.capitalize
.capitalize
method justupcase
s the first letter of the string, not the first letter of every word.