1

the problem occurs when I put the inputs (n1, n2) and the console still request me for more inputs.

my code:

#!/usr/bin/env ruby

def Euclides(n1,n2)
    while n1 % n2 != 0
        aux = n1
        n1 = n2
        n2 = aux % n2
    end
    return n2
end

n1 = gets
n2 = gets
puts Euclides(n1,n2)
3
  • What is your question? Commented May 24, 2014 at 18:56
  • The problem is not with gets, I just tested. Your code is falling infinite loop, thus it seems to you. Just put puts 123 before the loop, you can see that, control goes there. Commented May 24, 2014 at 19:03
  • not is infty loop, try with 2 values in function, like this: puts Euclides(5,10) Commented May 24, 2014 at 19:12

1 Answer 1

1

Your problem is that what you get from gets is a string. Passing two strings to your method results in an infinite loop since string1 % string2 will always return string1 (unless you have some special syntax in that string, see % documentation for string.

To solve you problem, you should convert your strings to integers:

n1 = gets.to_i
n2 = gets.to_i
puts Euclides(n1,n2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.