-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariables.rb
69 lines (47 loc) · 937 Bytes
/
variables.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
# Duck typing
# Ruby doesn't require strong typing like in languages like C++, C# and Java.
# Ruby is smart enough figure out the type of the variable.
# This is called Dynamic Typing or Duck Typing mean if it looks like a duck and waddles like a duck then it must be a duck
x = "this is a string"
puts x.class
x = 100
puts x.class
x = x.to_f
puts x.class
# Local
# Variables & Scope
# Global
$hates_fleas = true
# Class
class Dog
@@count = 0
def initialize(name)
@name = name
@@count += 1
end
def name
@name
end
def Dog.count
@@count
end
end
rover = Dog.new("Rover")
snoopy = Dog.new("Snoopy")
puts rover.name
puts snoopy.name
puts "I have #{Dog.count} dogs"
# Instance & Local Variables
class Dog
def initialize(name)
@name = name
end
def name
@name
end
end
rover = Dog.new("Rover")
snoopy = Dog.new("Snoopy")
puts rover.name
puts snoopy.name
# Loops & Variable Scope