Forum: Ruby Working with loops

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
076a2b44d85eb8731301da032cc4280c?d=identicon&s=25 GLENN CHAMBERLAIN (rubynewb)
on 2017-03-04 20:38
Hello everyone,

So I made a script that will take 3 numbers from the user and provide
the sum and product of them. What I want to do is give the option for
the user to redo the process with a new set of number. It does restart
the process of asking for inputs but the problem is the variable is
already assigned from the previous run through (iteration?). Is there a
command to force the script to start from scratch and not carry over
previous inputs?



class Screen
  def cls
    puts ("\n" * 25)
    puts "\a"
  end

  def pause
    STDIN.gets
  end


#Logic



  Console_Screen = Screen.new
  puts "Please enter a set of 3 numbers after pressing enter to get both
a product and sum."



 Console_Screen.pause
  loop do
print 'First : '
First  = gets.to_i

print 'Second : '
Second  = gets.to_i

print 'Third : '
Third  = gets.to_i

sum = First  + Second  + Third
product = First  * Second  * Third
puts "Product: #{product}"
puts "Sum: #{sum}"
 puts "Would you like another calculation?(y/n)"


answer = gets
answer.chop!

if answer == "y"

#--------------------------------------what i think the problem is
  redo

  #------------------------------------------------


  else

  puts "Have fun doing mental math."
  Console_Screen.pause
 exit


end
end

end
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-03-05 17:44
(1) You can encapsulate everything in a method or block, so that the
variables are local.

(2) You can explicitly assign nil to them in order to reset the
variables.
This topic is locked and can not be replied to.