Railstutorial (Michael Hartl): Exercise 4.6

methods, ruby, ruby-on-rails, split, syntax

Solution

I think just adding shuffle and join like you said should work:

def string_shuffle(s)
  s.split('').shuffle.join('')
end

The methods work from left to right. The split('') splits the word (s) into an array of separate letters. Shuffle then randomizes them. And join('') reverses the split, and puts it back into one word.

edit for clarification: The ('') is two single quotes, not one double quote. It should also work if you did split("") and join(""), since you're just trying to split and join on each character.

Problem

I don't get the answer to the following exercise from Hartl's Rails tutorial: By replacing the question marks in Listing 4.10 with the appropriate methods, combine split, shuffle, and join to write a function that shuffles the letters in a given string. Listing 4.10: ``` >> def string_shuffle(s) >> s.split('').?.? >> end => nil >> string_shuffle("foobar") ``` Can someone please help me? Thanks a lot in advance!

Original source