Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Created October 14, 2016 20:18
Show Gist options
  • Save dmmfll/10d9dfbfaadedf6acd59835fbb6df8ba to your computer and use it in GitHub Desktop.
Save dmmfll/10d9dfbfaadedf6acd59835fbb6df8ba to your computer and use it in GitHub Desktop.
putting the "loop" into read-evaluate-print-loop
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