Need help with shuffle program

I am trying to make my own shuffle method instead of the build-in one.
From looking at the debugging program, the recursive_shuffle does not
return when unshuffled.length == 0. What went wrong there?

See code below:

[CODE]
class Array

def shuffle
return self if self.length < 1
recursive_shuffle self, []
end

def recursive_shuffle unshuffled, shuffled
random_index = rand(unshuffled.length)
shuffled << unshuffled.slice!(random_index)
return shuffled if unshuffled.index == 0
recursive_shuffle unshuffled, shuffled
end

end

words = [‘history’, ‘downstairs’, ‘adapter’, ‘mist’, ‘circumstance’,
‘coast’, ‘fender’, ‘war’, ‘alcohol’, ‘attendance’]
puts words.shuffle
[\CODE]

this line:
return shuffled if unshuffled.index == 0

I think you mean
return shuffled if unshuffled.length == 0

On Fri, Mar 29, 2013 at 10:40 AM, Vincent S.

Whoops, how can I not notice that? Thanks much.

On Mar 29, 2013, at 07:47 , Chris H. [email protected] wrote:

this line:
return shuffled if unshuffled.index == 0

I think you mean
return shuffled if unshuffled.length == 0

which could be made even more clear with:

return shuffled if unshuffled.empty?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs