Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.