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 trialandynguyen9
1,629 PointsRuby Loop -
- What is "fail" method here? How can it be used?
- Why there are 3 ends in the codes?
- Why repeat method isn't used if we take time to define it?
I give up!
def repeat(string, times)
fail "times must be 1 or more" if times < 1
counter = 0
loop do
# YOUR CODE HERE
end
end
Pablo Villegas
2,922 PointsPablo Villegas
2,922 Pointsfail
raises a Runtime error with the message you pass in (in this case only iftimes < 1
)def
statement needs anend
as well as theloop
statement. That is the syntax for rubyn.times.each { puts string }
wheren
istimes
in the above function andstring
is the string you want to repeat.