-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.rb
More file actions
100 lines (68 loc) · 1.5 KB
/
classes.rb
File metadata and controls
100 lines (68 loc) · 1.5 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
time = Time.new # The moment I generated this web page.
time2 = time + 60 # One minute later.
puts time
puts time2
puts Time.mktime(2000, 1, 1) # 2000 year.
puts Time.mktime(1976, 8, 3, 10, 11) # When I was born.
timeNow = Time.new
timeBorn = Time.mktime(1991, 3, 22)
puts timeDiff = ((timeNow - timeBorn) / 60 / 60 / 24 / 365).to_i.to_s + ' full years'
#Extending Classes Расширение классов
class Integer
def to_eng
if self == 5 # Внутри этого метода мы использовали self, чтобы ссылаться на объект (целое число), использующий этот метод.
english = 'five'
else
english = 'fifty-eight'
end
english
end
end
puts 5.to_eng
puts 58.to_eng
puts 45.to_eng
#Creating Classes
class Die
def roll
1 + rand(6)
end
end
# Let's make a couple of dice...
dice = [Die.new, Die.new]
# ...and roll them.
dice.each do |die|
puts die.roll
end
puts
#Instance Variables
class Die
def roll
@numberShowing = 1 + rand(6)
end
def showing
@numberShowing
end
end
die = Die.new
die.roll
puts die.showing
puts die.showing
die.roll
puts die.showing
puts die.showing
puts
class Die
def initialize
# I'll just roll the die, though we
# could do something else if we wanted
# to, like setting the die with 6 showing.
roll
end
def roll
@numberShowing = 1 + rand(6)
end
def showing
@numberShowing
end
end
puts Die.new.showing