forked from makersacademy/learn_to_program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn2program_ch09.rb
149 lines (117 loc) · 2.98 KB
/
learn2program_ch09.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
# 9.1: Method Parameters
=begin
puts "9.1: Method Parameters\n"
def say_moo(number_of_moos=1)
puts "mooooo...." * number_of_moos
end
say_moo(3)
def double_this(num)
num_times_2 = num * 2
puts num.to_s+' doubled is '+num_times_2.to_s
end
double_this(44)
# 9.4: Return Values
puts "\n9.4: Return Values\n"
def say_moo(number_of_moos)
puts 'mooooo...' * number_of_moos
'yellow submarine'
end
x = say_moo(3)
puts x.capitalize + ', dude...'
puts x + '.'
def favourite_food(name)
if name == 'Lister'
return 'vindaloo'
end
if name == 'Rimmer'
return 'mashed potatoes'
end
'hard to say...maybe fried plantain?'
end
def favourite_drink(name)
if name =='Jean-Luc'
'tea, Earl Grey, hot'
elsif name == 'Kathryn'
'coffee, black'
else
'perhaps...horchata?'
end
end
puts favourite_food('Rimmer')
puts favourite_food('Lister')
puts favourite_food('Cher')
puts favourite_drink('Kathryn')
puts favourite_drink('Oprah')
puts favourite_drink('Jean-Luc')
=end
=begin
# Psychology Experiment: Refactored
puts "\nPsychology Experiment: Refactored\n"
def ask(question)
while true
puts question
reply = gets.chomp.downcase
if (reply == 'yes' || reply == 'no')
if reply == 'yes'
answer = true
else
answer = false
end
break
else
puts "Please answer 'yes' or 'no'."
end
end
answer # This is what we reutrn (true or false).
end
puts "Hello, and thank you for..."
puts
ask("Do you like eating tacos?") # Ignore this return value
ask("Do you like eating burritos?") # And this one
wets_bed = ask("Do you wet the bed?") # Save this return value
ask("Do you like eating chimichangas?")
ask("Do you like eating sopapillas?")
puts "Just a few more questions"
ask("Do you like drinking horchata?")
ask("Do you like eating flautas?")
puts
puts "DEBRIEFING:"
puts "Thank you for..."
puts
puts wets_bed
=end
=begin
# 9.5.1: Psychology Experiment Refactored: Improved ask method
puts "\nPsychology Experiment Refactored: Improved ask method\n"
def ask(question)
while true
puts question
reply = gets.chomp.downcase
if (reply == 'yes' || reply == 'no')
if reply == 'yes'
return true
else
return false
end
break
else
puts "Please answer 'yes' or 'no'."
end
end
end
puts "Hello, and thank you for..."
puts
ask("Do you like eating tacos?") # Ignore this return value
ask("Do you like eating burritos?") # And this one
wets_bed = ask("Do you wet the bed?") # Save this return value
ask("Do you like eating chimichangas?")
ask("Do you like eating sopapillas?")
puts "Just a few more questions"
ask("Do you like drinking horchata?")
ask("Do you like eating flautas?")
puts
puts "DEBRIEFING:"
puts "Thank you for..."
puts
puts wets_bed
=end