DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
interludedesign profile image
Chris Swann

First try in Ruby, I think this can be done without splitting and rejoining.

def capitalizeFirstLast(string)

    string.split(' ').each do |word|
        word.downcase!
        word.sub!(/\A./) {|match| match.upcase} # First Char
        word.sub!(/.\z/) {|match| match.upcase} # Last Char
    end.join(' ')

end

puts capitalizeFirstLast "tHIs is my string"
# => ThiS IS MY StrinG