Ruby Ruby Loops Ruby Loops Loop Conditionals

Erik Bonn
Erik Bonn
3,624 Points

Ruby loops.How do I assign a value of a method to a certain variable, and use a break keyword to exit?

The question states: Using a loop construct, assign the value of the get_answer() method to an answer Variable. Use the break keyword to exit the loop if the answer is equal to the string e. Assume get_answer() is already written.

Assume get_answer() is already defined

I have this written so far.

def get_answer() answer = "" loop do print " What is the answer? " answer = gets.chomp break if answer = "e" end end

The question says that get_answer() is already defined, but when I don't include it, it says get_answer method not used.

loop.rb
# Assume get_answer() is already defined
def get_answer()
answer = ""
  loop do
    print " What is the answer? "
    answer = gets.chomp
    break if answer = "e"
    end 
  end
Curtis Dietz
Curtis Dietz
4,069 Points

I was getting "get_answer method not used" also

1 Answer

Angela Visnesky
Angela Visnesky
20,926 Points

Hi Erik, I think you are making the challenge more complex than it is. Usually the code challenges are very strict in their requirements. Here's the code I used to pass the challenge:

loop do
  answer = get_answer()
break if 
answer == "e"
end

I hope this helps!

Erik Bonn
Erik Bonn
3,624 Points

Thanks Angela! much easier! ha