Created
October 14, 2016 20:18
-
-
Save dmmfll/10d9dfbfaadedf6acd59835fbb6df8ba to your computer and use it in GitHub Desktop.
putting the "loop" into read-evaluate-print-loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
first_loop = true | |
loop do | |
# Trap ^C aka interrupt signal | |
Signal.trap("INT") { | |
puts "\nGoodbye!" | |
exit | |
} | |
[ | |
"This loop echos everything you input.", | |
"Type <Ctl-c> to exit.", | |
].each do |line| | |
puts line | |
end if first_loop | |
# the last item of array is the return of `gets` | |
response = [ | |
print("Enter your text: "), # the prompt, returns nil | |
gets.chomp # returns the users input minus the end of line char | |
].last | |
puts response # prints response to screen | |
first_loop = false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment