Program 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]

···

--
Posted via http://www.ruby-forum.com/.

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 Stowbunenko <lists@ruby-forum.com>wrote:

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]

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/.

which could be made even more clear with:

return shuffled if unshuffled.empty?

···

On Mar 29, 2013, at 07:47 , Chris Hulan <chris.hulan@gmail.com> wrote:

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

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