-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch13_class.rb
60 lines (47 loc) · 1018 Bytes
/
ch13_class.rb
1
2
3
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#Chapter 13.1
class Array
def shuffle
arr = self
# Now we can just copy the old shuffle method
shuf = []
while arr.length > 0
#Randomly pick one element
rand_index = rand(arr.length)
curr_index = 0
new_arr = []
arr.each do |item|
if curr_index == rand_index
shuf.push item
else
new_arr.push item
end
curr_index = curr_index + 1
end
arr = new_arr
end
shuf
end
end
class Integer
def factorial
if self <= 1
1
else
self * (self-1).factorial
end
end
def to_roman
roman = ''
roman = roman + 'M' * (self /1000)
roman = roman + 'D' * (self % 1000 / 500)
roman = roman + 'C' * (self % 500 / 100)
roman = roman + 'L' * (self % 100 / 50)
roman = roman + 'X' * (self % 50 / 10)
roman = roman + 'V' * (self % 10 / 5)
roman = roman + 'I' * (self % 5 / 1)
roman
end
end
puts [1,2,3,4,5].shuffle
puts 7.factorial
puts 73.to_roman