I'm trying to do some string manipulation on a string in Ruby. The goal is to strip, reverse, squeeze and upcase only the first 100 characters without affecting the rest of the string.
Here is the string we will work with. The line numbers are part of the string. In the assignment this string is referred to as 'the_string'.
1. this string has leading space and too "MANY tabs and sPaCes betweenX"
2. thE indiVidual Words in each Line.X
3. eacH Line ends with a accidentally aDDED X.X
4. in this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5. ("nOrMaLiZiNg" means capitalizing sentences and setting otherX
6. characterS to lower case) and removes the extra spaces between WOrds.X
Here's what I've got working:
puts the_string[0,100].strip.squeeze.reverse.upcase
and the output:
I EHT .2
"XNEWTEB SECAPS DNA SBAT YNAM" OT DNA ECAPS GNIDAEL SAH GNIRTS SIHT .1
This is working the way I want it to, except rather than remove the remaining characters from the string (after 100), I want them to remain in place, and unaltered. Additionally, I'm not supposed to modify the object_id, and so therefore I cannot create a new string to solve this problem. The output I seek is:
I EHT .2
"XNEEWTEB SECAPS DNA SBAT YNAM" OOT DNA ECAPS GNIDAEL SAH GNIRTS SIHT .1ndiVidual Words in each Line.X
3. eacH Line ends with a accidentally aDDED X.X
4. in this lab you wilL WRITE code that "sAnITizES" this string by normalizingX
5. ("nOrMaLiZiNg" means capitalizing sentences and setting otherX
6. characterS to lower case) and removes the extra spaces between WOrds.X
I am certain that there is a method that makes this easy, I just haven't discovered the elegant solution. Any help is greatly appreciated!