Ruby는 do-while문이 있나요?
조회수 2092회
빈 문자열이 입력되기 전까지 사람 이름을 입력받고, 그걸 array에 저장하는 프로그램을 만들고 있습니다.
빈 문자열을 검사할 때 그냥 while
문을 쓰려고 하니 미리 문자열을 저장할 변수에 다른 문자를 저장해야되고, 또 while문 안에서만 쓸 변수를 밖으로 빼내야합니다.
people = []
info = 'a' # while문 바깥에 써야하고, 다른 값을 미리 넣어줘야함
while not info.empty?
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end
이렇게 do-while이 있으면 좀 더 간단해 질 것 같은데
people = []
do
info = gets.chomp
people += [Person.new(info)] if not info.empty?
while not info.empty?
ruby에는 do-while()
문을 어떻게 쓸 수있는지요
1 답변
-
Ruby언어 디자이너인 Yukihiro Matsumoto는
begin <code> end while <condition>
대신 Kernel#loop을 권장했습니다loop do # some code here break if <condition> end
댓글 입력