4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/simplescrambler.rb', line 4
def scramble(min = 10, max = 100)
if max.class != Integer || min.class != Integer
raise NotNumber
elsif self.length == 1
raise CannotScrambleStringLetter
elsif self.chars.count(self.chars[0]) == self.length
raise CannotScrambleStringSame
elsif min > 0 && max >= min
temp = self.chars
(min + rand(max - min + 1)).times do
random = rand(temp.length)
random2 = rand(temp.length)
if random2 == random
until random2 != random
random2 = rand(temp.length)
end
end
onepos = random
random = temp[random]
twopos = random2
random2 = temp[random2]
temp[onepos] = random2
temp[twopos] = random
end
return temp.join
elsif min <= 0
raise TooSmall
elsif max < min
raise MinMaxMismatch
end
end
|