Skip to content

Instantly share code, notes, and snippets.

@nossidge
Created April 15, 2018 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nossidge/fc90c767e5ddd5767a2c784a5a80264a to your computer and use it in GitHub Desktop.
Save nossidge/fc90c767e5ddd5767a2c784a5a80264a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Solution to the below question:
# https://codereview.stackexchange.com/questions/191522
# Initialise a variable to store the accumulating total.
total = 0
# Start an infinite loop. (When writing these, make sure there's a
# 'break' command somewhere within the loop, or it will loop forever)
loop do
# Prompt the user for their input.
puts 'give me a number'
# Get the user input and chomp to remove trailing white-space.
input = gets.chomp
# Break out of the loop if 'stop' was input.
break if input == 'stop'
# Now that we know the input String is supposed to be a number,
# we can convert it to Integer, and add it to the total.
total += input.to_i
end
# Output the final total.
puts total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment