Search code examples
ruby

New to Ruby - how do I shuffle a string?


Want to shuffle a string. This is my code: what is wrong about it? Thanks.

>> def string_shuffle(s)
>>   s.split('').shuffle(s.length()).join
>>   return s
>> end

Solution

  • If understand you correctly, you want this:

    def string_shuffle(s)
      s.split("").shuffle.join
    end
    
    string_shuffle("The Ruby language")
    => "ea gu bgTayehRlnu"