Beginner trouble with loops

I’m terribly new to Ruby, and I just can’t figure out what I’m doing
wrong with my code. I’ve been staring at it for 15 minustes now, without
coming to any conclusion.

What I want the code to do ask for the reader’s name. And if the reader
fills in anything blank. It will ask the same question again

loop do
puts “You’re starting to remember your name. What is it?”
name = gets.chomp.upcase
break if name != “”
end
puts “Yeah, that’s right! Your name is #{name}.”
gets

The looping works perfectly fine, but if the user-input is anything else
than “”, the program stops.

Did you read the error message?
the variable “name” is defined inside the loop, but you’re trying to
access it after the loop finishes.
If you want “name” to be available outside the loop, declare it before
the loop starts:

name = ‘’
loop do

The problem is that you wrote ‘!=’ instead of ‘==’.
Try this!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs