Lesson 1
String Manipulation and Reversal
Introduction

Hello, and welcome! Are you ready to enhance your string manipulation skills with Ruby?

Today, we’ll tackle a fun and practical task: reversing the characters in each word of a string while maintaining the original word order. This exercise will deepen your understanding of Ruby string methods and sharpen your problem-solving skills. Let’s get started!

Task Statement and Description

The goal is to write a Ruby method that takes a string as input, reverses each word within the string, and returns a new string with the reversed words in their original order.

Here’s what you need to know:

  • The input string will have between 1 and 100 words.
  • Words are separated by single spaces and consist of characters ranging from a to z, A to Z, 0 to 9, or underscores _.
  • There will be no leading or trailing spaces, and double spaces won’t appear.
  • The returned string should contain the reversed words in their original order, separated by single spaces.

Example

If the input string is "Hello neat rubyists_123", the function should:

  1. Reverse "Hello" to "olleH", "neat" to "taen", and "rubyists_123" to "321_stisybur".
  2. Combine these reversed words into a single string, producing "olleH taen 321_stisybur".

Try solving the problem on your own first! Once you're ready, we’ll walk through the solution step by step and break it down together.

Step 1: Splitting the String into Words

The first step is to break the input string into words. Ruby’s split method is perfect for this, as it divides the string based on spaces by default and returns an array of words.

Ruby
1input_str = "Hello neat rubyists_123" 2 3# Split the string into an array of words 4words = input_str.split 5 6puts words.inspect

This will output:

Ruby
1["Hello", "neat", "rubyists_123"]
Step 2: Reversing Each Word

Once the words are separated, we can reverse each one. Ruby’s reverse method makes this easy. Using map, we can apply reverse to every word in the array.

Ruby
1# Reverse each word 2reversed_words = words.map { |word| word.reverse } 3 4puts reversed_words.inspect

This produces:

Ruby
1["olleH", "taen", "321_stisybur"]
Step 3: Joining the Reversed Words

The final step is to combine the reversed words back into a single string. Ruby’s join method allows us to do this while specifying a separator—in this case, a single space (' ').

Ruby
1# Join the reversed words with a space separator 2result = reversed_words.join(' ') 3 4puts result

This outputs:

Ruby
1"olleH taen 321_stisybur"
Complete Method

Now, let’s bring everything together into a reusable method:

Ruby
1def reverse_words(input_str) 2 # Split the string into words 3 words = input_str.split 4 5 # Reverse each word 6 reversed_words = words.map { |word| word.reverse } 7 8 # Join the reversed words into a single string 9 reversed_words.join(' ') 10end 11 12# Example usage 13puts reverse_words("Hello neat rubyists_123") # Outputs: "olleH taen 321_stisybur"

This method is concise, efficient, and adheres to Ruby best practices.

Lesson Summary

Great job! You’ve just learned how to reverse characters in each word of a string while maintaining the original word order. By combining Ruby’s split, map, and join methods, you’ve created a powerful tool for string manipulation.

Remember, the key to mastery is regular practice. Therefore, take a moment to explore related problems and practice what you’ve learned. It's all part of the joy of learning!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.