This repository has been archived by the owner on Nov 25, 2017. It is now read-only.
forked from GracieE/BEWDiful_Students
-
Notifications
You must be signed in to change notification settings - Fork 22
/
scope.rb
162 lines (124 loc) · 4.1 KB
/
scope.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#Explaining: Variable Scope.
# methods create scope
#AVOID GLOBAL VARIABLES
chant = "Go Mets!"
# def shout
# puts "About to shout"
# puts chant.upcase
# puts "My throat hurts"
# end
# shout
def shout(chant)
puts "About to shout"
puts chant.upcase
puts "My throat hurts"
end
shout(chant)
#Anything you want to use has to be passed inject
#####################################################################################################
#####################################################################################################
#####################################################################################################
# chant = "Go Yankees!"
# def shout(chant)
# puts "About to shout"
# puts chant.upcase
# puts "My throat hurts"
# end
# shout(chant)
# Our next structure is a class
#####################################################################################################
#####################################################################################################
#####################################################################################################
# class Car
# def fill_er_up
# @gasoline = 10
# end
# def has_gasoline?
# @gasoline > 0
# end
# def start_car!
# if has_gasoline?
# puts "vroom!".upcase
# end
# end
# end
# mazda = Car.new
# mazda.fill_er_up
# mazda.start_car!
# We saw that methods keep their own scope. We need to use a different kind of variable to give it a greater scope
#####################################################################################################
#####################################################################################################
#####################################################################################################
# class Car
# def fill_er_up
# @gasoline = 10
# end
# def has_gasoline?
# @gasoline > 0
# end
# def start_car!
# if has_gasoline?
# puts "vroom!".upcase
# end
# end
# end
# mazda = Car.new
# mazda.fill_er_up
# mazda.start_car!
# Warning, the next variable isn't really used much
#####################################################################################################
#####################################################################################################
#####################################################################################################
#Class variables @@cars_created
# class Car
# @@cars_created = 0
# def initialize
# @@cars_created += 1
# end
# def show_cars_created
# puts "#{@@cars_created} cars created!"
# end
# end
# mazda = Car.new
# toyota = Car.new
# honda = Car.new
# honda.show_cars_created
# Constants - Intended to never be changed. Think Pi.
#####################################################################################################
#####################################################################################################
#####################################################################################################
class Car
WHEELS = 4
end
puts "Cars have #{Car.WHEELS} wheels"
Needs the scope resolution operator. This just means "Go into this class and grab this constant". Only works with constants
class Car
WHEELS = 4
end
puts "Cars have #{Car::WHEELS} wheels"
# THERE IS RARELY A REASON TO USE THIS TYPE OF VARIABLE!
#####################################################################################################
#####################################################################################################
#####################################################################################################
# $sharing_too_much = "We're all friends here"
# class Nosy
# def look_around
# puts $sharing_too_much
# end
# end
# class BadBoy
# def tamper
# $sharing_too_much = "You look fat in those jeans"
# end
# end
# class Busybody
# def look_around
# puts $sharing_too_much
# end
# end
# nosy = Nosy.new
# bad_boy = BadBoy.new
# busy_body = Busybody.new
# nosy.look_around
# bad_boy.tamper
# busy_body.look_around