-
Notifications
You must be signed in to change notification settings - Fork 0
/
L4-conditionals-loops.rb
91 lines (70 loc) · 1.42 KB
/
L4-conditionals-loops.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
# Conditionals
is_this_thing_on = true
if is_this_thing_on
puts "Yep! It's on!"
end
# the shorter version
puts "Yep! It's on!" if is_this_thing_on
#what about the opposite?
puts "turn it on" unless is_this_thing_on
#what else can we do?
if is_this_thing_on
puts "It's on!"
else
puts "Turn it on"
end
def grade_paper(grade)
case grade
when "A"
puts 'Well done!'
when "B"
puts 'Try harder!'
when "C"
puts 'You need help!!!'
else
puts "You just making it up!"
end
end
def grade_test(grade)
case grade
when "A", "B"
puts 'You pretty smart!'
when "C", "D"
puts 'You pretty dumb!!'
else
puts "You can't even use a computer!"
end
end
# What about loops?
i = 0
while i < 5 do
puts i
i += 1
end
for animal in ["dog", "cat", "rat", "bat"] do
puts "I just saw a #{animal}"
end
# Blocks
(1..10).each {|i| puts i}
(1..10).each {|i| puts i if i % 2 == 1}
# each implemented in ruby?
def each(array, &block)
for i in array do
yield i
end
end
# use it to find the odd numbers
each([5,6,7,8]) {|num| puts num % 2 == 1 ? "odd" : "even"}
# use blocks for setup and cleanup
def read_line(file, &block)
#implement
end
#show operators
def <<(track)
end
#show named parameters
# lets revisit the Playlist class so that it works with the following
# code
playlist.play({:artist => "Dylan", :rating => 3}) do |track|
puts "I am playing #{track.name} by #{track.artist}"
end