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!
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
toz
,A
toZ
,0
to9
, 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:
- Reverse
"Hello"
to"olleH"
,"neat"
to"taen"
, and"rubyists_123"
to"321_stisybur"
. - 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.
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.
Ruby1input_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:
Ruby1["Hello", "neat", "rubyists_123"]
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.
Ruby1# Reverse each word 2reversed_words = words.map { |word| word.reverse } 3 4puts reversed_words.inspect
This produces:
Ruby1["olleH", "taen", "321_stisybur"]
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 (' '
).
Ruby1# Join the reversed words with a space separator 2result = reversed_words.join(' ') 3 4puts result
This outputs:
Ruby1"olleH taen 321_stisybur"
Now, let’s bring everything together into a reusable method:
Ruby1def 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.
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!