• 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 String

Using Ruby to Capitalize First Letter of String

November 30, 2022 Leave a Comment

We can use Ruby to capitalize the first letter of a string by using the Ruby capitalize method.

some_string = "how are you?"
some_string.capitalize

Let’s look at a simple example below.


Let’s say we have a string and we simply want to capitalize the first letter in that string. To do this we can use our code from above.

some_string = "how are you?"
some_string = some_string.capitalize

puts some_string

#Output
How are you?

We can also use Ruby to capitalize every word of a string. Let’s take a look at that below.

Using Ruby to Capitalize Every Word in a String

To capitalize every word in a string, we will need to make use of the Ruby split, join, and map methods. We have a whole post on how to do this, but we will go over it again here.

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(" ")

puts cap_string

#Output:
This Is A String With Some Words

In our code 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.

We can put this code in a method so that we can reuse it whenever we want.


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"

puts 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 a string.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Create List of Prime Numbers
  • 2.  Using Ruby to Convert String to Array
  • 3.  Using Ruby to Capitalize First Letter of Each Word
  • 4.  Ruby Floor – Find Floor of Number with floor Method
  • 5.  How to Square a Number in Ruby
  • 6.  Using Ruby to Convert Array to String
  • 7.  Using Ruby to Get the Length of a String
  • 8.  Ruby include Method – Check if Item in Array
  • 9.  Ruby has_value Method – Check if Hash Has Value
  • 10.  Using Ruby to Get the Length 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