0

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
Dym
  • 53
  • 6
  • "I'd like to loop it" So loop it. There is no loop at all in your code. – matt Aug 29 '20 at 01:05
  • Well, honestly, that's where I'm stuck. I've tried multiple ways to get the bottom half of the code to loop. I'm not clear on how to implement it. I've been reading blogs and guides all night. My eyes are crossed. Probably going to set away from it for a bit. – Dym Aug 29 '20 at 01:42
  • "I've tried multiple ways to get the bottom half of the code to loop" Well show us _one_ of them. – matt Aug 29 '20 at 01:54
  • Updated! This is one thing I've tried. But this is beyond me right now. – Dym Aug 29 '20 at 02:05
  • Well you're missing an `end`. – matt Aug 29 '20 at 02:07
  • And then the problem is that there is nothing _inside_ the `until` loop that would ever change `math`, so you just loop forever. – matt Aug 29 '20 at 02:12

2 Answers2

1

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

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

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.

Pascal
  • 8,464
  • 1
  • 20
  • 31
Lana Dz
  • 71
  • 6
  • Thanks for jumping in here. This doesn't quite work for my situation as it is. Or I missing something. I'll share my full code, incase that clarifies what I'm trying to do here. But I forgot about the \n command for adding a line. Thanks for that! – Dym Aug 29 '20 at 01:31