I'm creating a program that receives a person's name and stores it in an array until an empty string is entered.
When checking for empty strings, you just want to write while
statements, so you need to store other characters in the variable to store the string in advance, and you need to take out the variable to write only within the while statement.
people = []
info = 'a' # while must be written outside the statement, and other values must be entered in advance
while not info.empty?
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end
I think it'll be simpler if there's a do-while like this
people = []
do
info = gets.chomp
people += [Person.new(info)] if not info.empty?
while not info.empty?
For ruby, how can you write do-while()
statements
Ruby language designer Yukihiro Matsumoto recommended Kernel #loop instead of begin <code> end while <condition>
loop do
# # some code here
break if <condition>
end
© 2024 OneMinuteCode. All rights reserved.