Does Ruby have a do-while statement?

Asked 1 years ago, Updated 1 years ago, 151 views

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

loops ruby

2022-09-22 22:08

1 Answers

Ruby language designer Yukihiro Matsumoto recommended Kernel #loop instead of begin <code> end while <condition>

loop do 
  # # some code here
  break if <condition>
end 

Related documents


2022-09-22 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.