# How to capitalize each word?

**URL:** <https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474>\
**Category:** Development\
**Created:** [May 4, 2020, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474 "2020-05-04T23:04:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 4, 2020, 11:04pm UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474/1 "2020-05-04T23:04:01Z")

</div>

![Ruby2](https://www.ruby-forum.com/uploads/default/original/2X/a/ae07fbe25a2640b39489271a79e991c45df6fdbf.png)

How to also capitalize ‘angeles’ ?

I tried .split, .map, .join, .titleize

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 5, 2020, 12:31am UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474/2 "2020-05-05T00:31:50Z")

</div>

What are you doing at the moment? Are you using capitalize?  
[https://docs.ruby-lang.org/en/2.7.0/String.html#method-i-capitalize](https://docs.ruby-lang.org/en/2.7.0/String.html#method-i-capitalize)

To apply to all words in a string, you have to split the string into words, apply your change, and then rejoin the array:

```auto
2.7.1 :001 > x = "los angeles"
2.7.1 :002 > y = x.split(' ').collect(&:capitalize).join(' ')
2.7.1 :003 > y
 => "Los Angeles"

```

---

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 5, 2020, 12:58am UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474/3 "2020-05-05T00:58:24Z")

</div>

```
# Prompt the user for input. #

```

print “What’s your first name?\n”  
first\_name = gets.chomp

print “What’s you last name?\n”  
last\_name = gets.chomp

print “What’s your city?\n”  
city = gets.chomp

print “What’s your state’s abbreviation?\n”  
state = gets.chomp

# Formatting the output with string methods.

first\_name.capitalize!  
last\_name.capitalize!  
city = city.split(’ ‘).collect(&:capitalize).join(’ ')  
state.upcase!

# Print the Output.

puts “Your first name is #{first\_name}”  
puts “Your last name is #{last\_name}”  
puts “Your city is #{city}”  
puts “Your state is #{state}”

# End of script.

---

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 5, 2020, 12:58am UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474/4 "2020-05-05T00:58:59Z")

</div>

Is ‘city’ right now ?

---

<div class="post-metadata">

**Author:** ![PaulGureghian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba8739/32.png) [@PaulGureghian](https://www.ruby-forum.com/u/PaulGureghian)\
**Post date:** [May 5, 2020, 1:32am UTC](https://www.ruby-forum.com/t/how-to-capitalize-each-word/259474/5 "2020-05-05T01:32:21Z")

</div>

That worked. thanks for the help.
