Dym
Dym

Reputation: 53

Ruby - I have code that I like but I would like it to loop until one of the conditions is true

So I'm trying to understand the loop functions in ruby. I have a chunk of code that does what I want it to do. But I'd like to loop the section that starts with the math variable until one of the conditions is met. I've updated this to show one way I've tried. Looked here and on (https://launchschool.com/books/ruby/read/loops_iterators) but I'm not understanding the process. I'm really new to this. If you have a resource I could reference that would be awesome. If you have a solution with tips on why it works even better. This is what I've tried. It's probably laughably bad.

puts "Welcome to math.rb!"
puts "Enter a number!"
     user = gets.to_i
puts "Enter another number!"
    user2 = gets.to_i
puts "What would you like to do with your number?"
math = gets.chomp.downcase
until math == ["add", "subtract", "multiply"]
case math
when "add"
    puts user + user2
when "subtract"
    puts user - user2
when "multiply"
    puts user * user2
else
    puts "I don't understand! Type a command like:
-add
-subtract
-multiply"
end

Upvotes: 0

Views: 121

Answers (2)

Timur Shtatland
Timur Shtatland

Reputation: 12347

Put an infinite loop around the code using loop operator. Put next after the "bad" condition to go back to the beginning of the loop. In all other cases, the code falls through to the final break operator before the end of the infinite loop, which causes the loop to exit. If you do not need to reuse math variable, you can get rid of it altogether. Here it is used only once, so I remove it, and instead gets.chomp.downcase is directly evaluated in the case statement without the need of a temporary variable.

#!/usr/bin/env ruby

puts "Welcome to math.rb!"
puts "Enter a number!"
     user = gets.to_i
puts "Enter another number!"
    user2 = gets.to_i
puts "What would you like to do with your number?"

loop do
  case gets.chomp.downcase
  when "add"
    puts user + user2
  when "subtract"
    puts user - user2
  when "multiply"
    puts user * user2
  else
    puts "I don't understand! Type a command like:
-add     
-subtract               
-multiply"
    next
  end
  break
end

SEE ALSO:

loop is the most accepted and common way to create infinite loops in Ruby : Creating an Infinite Loop
loop is a kernel method which takes a block, which introduces a new local variable scope in the infinite loop. This is unlike while true infinite loop, which does not introduce a new scope. Thus, while true may leak variables outside of the loop, which may be unexpected and lead to subtle bugs later: https://stackoverflow.com/a/45070639/967621

Upvotes: 1

Lana Dz
Lana Dz

Reputation: 81

I guess you are building command-line like tool, right? So, what you can use Kernel#loop

loop do  # begin of loop
  math = gets.chomp.downcase
  case math
  when "add"
    puts user + user2 
  when "subtract"
    puts user - user2
  when "multiply"
    puts user * user2
  when "exit"
    break    # here is your conditional to break from loop
  else
    puts "I don't understand! Type a command like: \n-add\n-subtract\n-multiply"
  end
end

I haven't tried to run it, let me know if it works.

Upvotes: 0

Related Questions