Ruby Ruby Loops Ruby Loops The Ruby Loop

Alex Smith
Alex Smith
8,121 Points

How do you do this?

Can someone please explain this to me?

loop.rb
def repeat(string, times)
  fail "times must be 1 or more" if times < 1
  counter = 0
end

loop do
    print repeat(string)
    counter += 1

    if repeat('counter') == repeat('times')
      break
    end

end

1 Answer

Michael Hall
PRO
Michael Hall
Pro Student 27,994 Points

Can you be more specific? as it is this code would not run. The first error is on the line where you call repeat(string). You have defined the repeat method to take 2 arguments, but you are only passing one. counter has scope limited to the method in which it is defined, you can not access it outside this method. counter += 1 will throw an error. To solve this, move it outside the method. If you need to use it from within the method, pass it in as an argument.