Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
PLUS
Michael Hall
Courses Plus Student 30,909 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.