# Capitalizing first letter in each line of a string

**URL:** <https://www.ruby-forum.com/t/capitalizing-first-letter-in-each-line-of-a-string/202970>\
**Category:** Ruby\
**Created:** [February 9, 2011, 9:33pm UTC](https://www.ruby-forum.com/t/capitalizing-first-letter-in-each-line-of-a-string/202970 "2011-02-09T21:33:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 9, 2011, 9:33pm UTC](https://www.ruby-forum.com/t/capitalizing-first-letter-in-each-line-of-a-string/202970/1 "2011-02-09T21:33:38Z")

</div>

Hi,  
Im in need of capitalizing first letter of each line in a long string,  
and have  
it print exactly the same (but with the first word capitalized+with the  
whitespace)… but cant figure it out… any advise?

taco= \<\<-HERE  
to bad I hate tacos theytaste great…  
so much for a HEALTHY diet…

HERE

taco.scan(/\W+._\w+._/){|x| print x.capitalize}

* * *

im guessing my regular expression is wrong. and possibly the .capitalize  
is in  
the wrong space?  
sorry im a newbie 🙂  
thanks  
rich

---

<div class="post-metadata">

**Author:** ![nuxygen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@nuxygen](https://www.ruby-forum.com/u/nuxygen)\
**Post date:** [February 9, 2011, 9:45pm UTC](https://www.ruby-forum.com/t/capitalizing-first-letter-in-each-line-of-a-string/202970/2 "2011-02-09T21:45:33Z")

</div>

On Thu, 10 Feb 2011 05:32:50 +0900  
Richard P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> HERE
> 
> taco.scan(/\W+._\w+._/){|x| print x.capitalize}
> 
> * * *
> 
> im guessing my regular expression is wrong. and possibly  
> the .capitalize is in the wrong space?  
> sorry im a newbie 🙂  
> thanks  
> rich

Here you are:

taco = \<\<-HERE  
to bad I hate tacos they taste great..  
so much for a HEALTHY diet…

HERE

p taco.gsub(/^(\W\*?[a-z])/) { |m| m.upcase }

---

<div class="post-metadata">

**Author:** ![nuxygen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@nuxygen](https://www.ruby-forum.com/u/nuxygen)\
**Post date:** [February 10, 2011, 7:30am UTC](https://www.ruby-forum.com/t/capitalizing-first-letter-in-each-line-of-a-string/202970/3 "2011-02-10T07:30:59Z")

</div>

thanks!

* * *

From: Peter Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wed, February 9, 2011 12:44:50 PM  
Subject: Re: capitalizing first letter in each line of a string.

On Thu, 10 Feb 2011 05:32:50 +0900  
Richard P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> HERE
> 
> taco.scan(/\W+._\w+._/){|x| print x.capitalize}
> 
> * * *
> 
> im guessing my regular expression is wrong. and possibly  
> the .capitalize is in the wrong space?  
> sorry im a newbie 🙂  
> thanks  
> rich

Here you are:

taco = \<\<-HERE  
to bad I hate tacos they taste great..  
so much for a HEALTHY diet…

HERE

p taco.gsub(/^(\W\*?[a-z])/) { |m| m.upcase }
