This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
Reversing a String by Words or Characters | ||
---|---|---|
Code | Expected | Actual |
s = ".sdrawkcab si gnirts sihT" s.reverse |
"This string is backwards." | "This string is backwards." |
s |
".sdrawkcab si gnirts sihT" | ".sdrawkcab si gnirts sihT" |
s.reverse! |
"This string is backwards." | "This string is backwards." |
s |
"This string is backwards." | "This string is backwards." |
s = "order. wrong the in are words These" s.split(/(\s+)/).reverse!.join('') |
"These words are in the wrong order." | "These words are in the wrong order." |
s.split(/\b/).reverse!.join('') |
"These words are in the wrong. order" | "These words are in the wrong. order" |
"Three little words".split(/\s+/) |
["Three", "little", "words"] | ["Three", "little", "words"] |
"Three little words".split(/(\s+)/) |
["Three", " ", "little", " ", "words"] | ["Three", " ", "little", " ", "words"] |