Some rules: must use capitalization (not upcase or any other expression)
Stuck on the following:
string = <<-HERE
i love tacos. I hear
they are delicious and nutritious
HERE
I need to capitalize the first word of each line with the whitespace and am having trouble figuring out how to get it done:
The output needs to look as follows:
I love tacos. I hear
They are delicious and nutritious
Any guidance or help will be greatly appreciated. I'll even take a point in the right direction rather than an answer!
Here's a one-liner that does what you asked for:
string.gsub(/^\s*\w/) {|match| match.upcase }
I know you said "no upcase", but in this context, it's only upcasing the first letter. Let me know if you have any questions about it.
And to address your comment on the other answer, you can always use gsub! to mutate the string in place without creating a copy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With