• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Capitalize First Letter of Each Word

Using Ruby to Capitalize First Letter of Each Word

October 17, 2022 Leave a Comment

In Ruby, we can capitalize the first letter of every word in a string easily with the help of the split and map methods, and the capitalize method. Let’s see the code for this first, and then go over what is happening.

some_string = "this is a string with some words"
words_array = some_string.split(" ")
cap_array = words_array.map { |word| word.capitalize }
cap_string = cap_array.join(" ")
print cap_string

#Output:
This Is A String With Some Words

We can put this code in a method that we will call capitalizeEachWord so that we can reuse this code whenever we want to capitalize all the words of a string.

def capitalizeEachWord(str)
  words_array = str.split(" ")
  cap_array = words_array.map { |word| word.capitalize }
  cap_string = cap_array.join(" ")
  return cap_string
end

And now let’s see our method in action with a simple example.

def capitalizeEachWord(str)
  words_array = str.split(" ")
  cap_array = words_array.map { |word| word.capitalize }
  cap_string = cap_array.join(" ")
  return cap_string
end

some_string = "this is a string with some words"

print capitalizeEachWord(some_string)

#Output:
This Is A String With Some Words

When using string variables in Ruby, we can easily perform string manipulation to change the value of the string objects. One such manipulation is to capitalize the first letter of every word in a string. We can easily capitalize the first letter of every word using Ruby.

In our method above, we first can use the split method to split the string by spaces to get an array of the words of the string object (convert the string to an array). We store this array in a variable called words_array. Then, we can loop over each word using the map method and call the capitalize method on each word in our array.

Finally, at the end, we can convert the array back to a string using the join method.

Below is our method again called capitalizeEachWord which will capitalize the first letter of each word in a string object.

def capitalizeEachWord(str)
  words_array = str.split(" ")
  cap_array = words_array.map { |word| word.capitalize }
  cap_string = cap_array.join(" ")
  return cap_string
end

some_string = "this is a string with some words"

print capitalizeEachWord(some_string)

#Output:
This Is A String With Some Words

Hopefully this article has been useful for you to learn how to use Ruby to capitalize the first letter of each word.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Create List of Prime Numbers
  • 2.  How to Print With Newline in Ruby
  • 3.  Ruby Floor – Find Floor of Number with floor Method
  • 4.  Ruby String Comparison
  • 5.  How to Square a Number in Ruby
  • 6.  Ruby has_value Method – Check if Hash Has Value
  • 7.  Using Ruby to Convert String to Boolean
  • 8.  Using Ruby to Reverse String
  • 9.  Using Ruby to Convert Array to String
  • 10.  Ruby unshift Method – Add Items to Start of Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy