0

I have a challenging Ruby problem:

I want to convert "howdy" to "YDWOh" and "how are you" to "uoy era WOh".

which is essentially capitalize, swapcase and reverse functions. Capitalize and swapcase is only for the first word of the sentence. How do I do this when I have more than one word in the sentence?

1
  • 4
    This doesn't seem like a normal situation. Is it homework or a programming challenge? If so, in either case, you should figure it out for yourself. That's the point of homework and programming challenges. Commented Apr 8, 2013 at 2:07

2 Answers 2

3

Try the following steps:

  1. capitalize your string
  2. split(' ') your string into an array with words
  3. Use each_with_index.map to go over each word in the array
  4. Use swapcase on only the first word of the array
  5. join(' ') the array back into a string
  6. reverse the string
Sign up to request clarification or add additional context in comments.

2 Comments

@Linuxios, but I didn't add the code on purpose... so that there was at least something left for him to figure out by himself.
Sorry. I'll revert. Someone bellow told me I should edit my answer of code into your answer as it was using your steps. I'll revert.
1

You can try the below:

a = "how are you" p a.gsub(a.split[0...1].join(' '),a.split[0...1].join(' ').capitalize.swapcase).reverse 

Output:

"uoy era WOh" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.