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

Luis Senior
Luis Senior
10,432 Points

in my workspaces when i type ruby loop.rb in console i don't get anything but a new line. it doesn't run loop

loop do

print "Do you want to continue? (y/n) "

answer = gets.chomp.downcase

end

1 Answer

Brandon McClelland
Brandon McClelland
4,645 Points

It looks like you'll want more logic to handle what to do with different answers.

if n, then break the loop if y (or anything else), then loop again

You could improve it by leaving a message when the loop breaks, or when you don't get input you expect.

For kicks I copied your code, saved it to a file named loop.rb and type 'ruby loop.rb' on a Debian Jessie based VM that uses Ruby 2.0.0 I've been using for development work and it works as expected, printing "Do you want to continue? (y/n) " every time I type something and hit enter.