Alternately, I would prefer to use Ruby#gsub and make it more of an object that it can be reusable
every other time. But before we get into that, you wanna read up on the following Ruby's String methods:
- String#gsub Method
- String#capitalize Method
- Read this blog too to have a view of how you can use gsub
I believe the method capitalize
and upcase
method can be used and you will see how I used it in my solution below. This will hand extra cases where you have camelcase in your Array, as well as Hyphen:
# frozen_string_literal: true
def converter(string)
string.capitalize.gsub(/\b[a-z]/, &:upcase)
end
array_name = ['the Dog', 'the cat', 'new york', 'SlACKoVeRFLoW', 'OlAoluwa-Afolabi']
print array_name.map(&method(:converter))
As you can see I changed the array a bit. I will advise you use string with single-quotes (i.e '')
and use double-quotes (i.e " ") when you want to do string concatenation
.
Note:
- I believe my solution can be extended further. It can be reviewed to suit all kinds of productivity and edge cases. So you can find such array and test with it, and extent the solution further.
- I used Ruby 2.7.1