what is dup?

D

David Madden

I am playing with an example in the ruby cookbook where a shuffle
function is added to the array class:

class Array

def shuffle!
each_index do |i|
j = rand(length-i) + i
self[j], self = self, self[j]
end
end

def shuffle
dup.shuffle!
end

end

What I don't understand is the line "dup.shuffle!"

What is the dup object?

Dave.
 
W

Wilson Bilkovich

I am playing with an example in the ruby cookbook where a shuffle
function is added to the array class:

class Array

def shuffle!
each_index do |i|
j = rand(length-i) + i
self[j], self = self, self[j]
end
end

def shuffle
dup.shuffle!
end

end

What I don't understand is the line "dup.shuffle!"

What is the dup object?


dup returns a copy (duplicate) of the object. In the above code, it is
used to let you get back a shuffled copy of the Array, without
shuffling the original.

type:
ri Object#dup
for more info.
 
D

David Madden

dup returns a copy (duplicate) of the object. In the above code, it is
used to let you get back a shuffled copy of the Array, without
shuffling the original.

type:
ri Object#dup
for more info.

Thanks,

Dave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top