-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.rb
More file actions
42 lines (35 loc) · 814 Bytes
/
Copy pathvariables.rb
File metadata and controls
42 lines (35 loc) · 814 Bytes
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
# global variables begin with a $
$count = 0
while $count <= 5 do
#the value of any variable can be interpolated into a string with #:
puts "#$count"
$count += 1
end
# instance variables are preceded by a @
class Point
def initialize(x, y)
@x = x
@y = y
end
def to_s
"(#@x, #@y)"
end
end
point = Point.new(5, -3)
puts point.to_s
# class variables (those that belong to a class and not to any
# instance of the class) are preceded by @@
class Counter
@@instance_count = 0
def initialize
@@instance_count += 1
end
def how_many
puts "#@@instance_count instances of Counter"
end
end
counter1 = Counter.new
counter1.how_many
counter2 = Counter.new
counter1.how_many
counter2.how_many