-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 3 | Variables
89 lines (61 loc) · 1.45 KB
/
Lesson 3 | Variables
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
Assigning values
---------------------------------------------
irb :001 > first_name = 'Joe'
=> "Joe"
irb :002 > first_name
=> "Joe"
irb :003 > a = 7
=> 7
Getting Data from a User
---------------------------------------------
irb :001 > name = gets
Bob
=> "Bob\n"
irb :001 > name = gets.chomp
=> "Bob"
irb :003 > name = gets.chomp
James
=> "James"
irb :004 > name + 'is my first name'
=> "James is super great!"
Variable Scope
---------------------------------------------
name = "something"
def print_full_name(first_name, last_name):
name = first_name + ' ' + last_na,e
puts name
end
print_full_name 'Peter', 'Henry'
print_full_name 'Alex', 'Johnson'
puts name
Types of Variables
---------------------------------------------
CONSTANT:
MY_CONSTANT = 'I am available throughout your app.'
GLOBAL:
$var = 'I am also available throughout your app.'
CLASS VARIABLE:
@@instances = 0
INSTANCE VARIABLE:
@var = 'I am available throughout the current instance of this class.'
LOCAL VARIABLE:
var = 'I must be passed around to cross scope boundaries.'
Exercises
---------------------------------------------
puts "what is your name?"
name = gets.chomp
puts "Hello" + name
puts "how old are you?"
age = gets.chomp.to_i
puts "in 10 years you will be:"
puts age + 10
puts "in 20 years you will be:"
puts age + 20
puts "in 30 years you will be:"
puts age + 30
puts "in 40 years you will be:"
puts age + 40
puts "what is your name?"
10.times do
puts name
end