Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/simplescrambler.rb

Instance Method Summary collapse

Instance Method Details

#isanagram?(base) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simplescrambler.rb', line 36

def isanagram?(base)
	if base.class != String
		raise NotString
	else
		one = self.chars
		two = base.chars
		if one.sort == two.sort
			return true
		else
			return false
		end
	end
end

#scramble(min = 10, max = 100) ⇒ Object



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