-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy path2_input_output_control.rb
More file actions
47 lines (40 loc) · 1.12 KB
/
Copy path2_input_output_control.rb
File metadata and controls
47 lines (40 loc) · 1.12 KB
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
# Prompt the user for a number, then read it in and print out "hi" that many times
#
# Repeat this process until the user submits "bye", then say "goodbye" and end the program
# HINT: Check out example 2 if you get stuck
# example:
# PROGRAM: Enter a number
# USER: 4
# PROGRAM: hi hi hi hi
# PROGRAM: Enter a number
# USER: 2
# PROGRAM: hi hi
# PROGRAM: Enter a number
# USER: bye
# PROGRAM: goodbye
# remember you can try your program out with $ ruby 2_input_output_control.rb
# and when you think it is correct, you can test it with $ rake 2:2
def input
puts "Enter your number or bye (if you wish to say goodbye)"
end
def hi_hi_goodbye
# your code here
puts input
while (num = gets) && (num !~ /bye/)
num.to_i.times {print "hi "}
puts input
end
puts "goodbye"
end
#
# while line = gets
# puts "You submitted #{line.inspect}"
# break if line.chomp == 'exit'
# end
#
# puts "Goodbye!"
#
# This will just invoke the method if you run this program directly
# This way you can try it out by running "$ ruby 2_input_output_control.rb"
# but it will still work for our tests
hi_hi_goodbye if $0 == __FILE__